Loop around dataframe columns and make a plotly of the columns that are present?2019 Community Moderator ElectionHow to sort a dataframe by multiple column(s)?R Shiny Plotly 3D : Change the title of x , y , z axis to actual variable name and add legend titleGenerate flexdashboard in RmarkdownPlotly, add border around points created with add_markersSharing R presentation with plotly iframesPlotly R chart from two different dataframeMake a line chart with plotly for multiple columnOrdering columns in Plotly horizontal bar chartHow to make plotly “scatter3d” respect order of factor levels in RHow to loop around table columns to get plotly traces?

is 'sed' thread safe

Why do phishing e-mails use faked e-mail addresses instead of the real one?

Is there a math equivalent to the conditional ternary operator?

Can an earth elemental drown/bury its opponent underground using earth glide?

What is better: yes / no radio, or simple checkbox?

Can a Trickery Domain cleric cast a spell through the Invoke Duplicity clone while inside a Forcecage?

In which way proportional valves are controlled solely by current?

PTIJ: Aharon, King of Egypt

Can we carry rice to Japan?

When do _WA_Sys_ statistics Get Updated?

Caulking a corner instead of taping with joint compound?

Ahoy, Ye Traveler!

Why doesn't "adolescent" take any articles in "listen to adolescent agonising"?

Is divide-by-zero a security vulnerability?

How do you say “my friend is throwing a party, do you wanna come?” in german

What is a term for a function that when called repeatedly, has the same effect as calling once?

Why are special aircraft used for the carriers in the United States Navy?

How to get the first element while continue streaming?

Practical reasons to have both a large police force and bounty hunting network?

Should we avoid writing fiction about historical events without extensive research?

Split a number into equal parts given the number of parts

Why would the IRS ask for birth certificates or even audit a small tax return?

I can't die. Who am I?

How to mitigate "bandwagon attacking" from players?



Loop around dataframe columns and make a plotly of the columns that are present?



2019 Community Moderator ElectionHow to sort a dataframe by multiple column(s)?R Shiny Plotly 3D : Change the title of x , y , z axis to actual variable name and add legend titleGenerate flexdashboard in RmarkdownPlotly, add border around points created with add_markersSharing R presentation with plotly iframesPlotly R chart from two different dataframeMake a line chart with plotly for multiple columnOrdering columns in Plotly horizontal bar chartHow to make plotly “scatter3d” respect order of factor levels in RHow to loop around table columns to get plotly traces?










0















I have a peice of code that produces a plotly graph but the issue is that each time I run this code not all the columns are necessarily there. At the minute I am just running the code till I get an error and then commenting out the add_trace part that is missing but this is getting tedious.



How can I set it up to look at the df and add the traces for only the columns that currently exist.



 graph <- plot_ly(Data, x = ~Weeknumber, y = ~Student, name = 'Student', type = 'scatter', mode = 'none', fill = 'tozeroy', fillcolor = red, hoverinfo = 'text',
text = ~paste('Date:', WeekStart)) %>%
add_trace(y = ~Senior, name = 'Senior', fillcolor = green) %>%
add_trace(y = ~Child, name = 'Child', fillcolor = blue) %>%
add_trace(y = ~Adult, name = 'Adult', fillcolor = purple) %>%
layout(title = 'Revenue',
xaxis = list(title = "Date",
showgrid = FALSE),
yaxis = list(title = "Revenue",
showgrid = FALSE))









share|improve this question


























    0















    I have a peice of code that produces a plotly graph but the issue is that each time I run this code not all the columns are necessarily there. At the minute I am just running the code till I get an error and then commenting out the add_trace part that is missing but this is getting tedious.



    How can I set it up to look at the df and add the traces for only the columns that currently exist.



     graph <- plot_ly(Data, x = ~Weeknumber, y = ~Student, name = 'Student', type = 'scatter', mode = 'none', fill = 'tozeroy', fillcolor = red, hoverinfo = 'text',
    text = ~paste('Date:', WeekStart)) %>%
    add_trace(y = ~Senior, name = 'Senior', fillcolor = green) %>%
    add_trace(y = ~Child, name = 'Child', fillcolor = blue) %>%
    add_trace(y = ~Adult, name = 'Adult', fillcolor = purple) %>%
    layout(title = 'Revenue',
    xaxis = list(title = "Date",
    showgrid = FALSE),
    yaxis = list(title = "Revenue",
    showgrid = FALSE))









    share|improve this question
























      0












      0








      0








      I have a peice of code that produces a plotly graph but the issue is that each time I run this code not all the columns are necessarily there. At the minute I am just running the code till I get an error and then commenting out the add_trace part that is missing but this is getting tedious.



      How can I set it up to look at the df and add the traces for only the columns that currently exist.



       graph <- plot_ly(Data, x = ~Weeknumber, y = ~Student, name = 'Student', type = 'scatter', mode = 'none', fill = 'tozeroy', fillcolor = red, hoverinfo = 'text',
      text = ~paste('Date:', WeekStart)) %>%
      add_trace(y = ~Senior, name = 'Senior', fillcolor = green) %>%
      add_trace(y = ~Child, name = 'Child', fillcolor = blue) %>%
      add_trace(y = ~Adult, name = 'Adult', fillcolor = purple) %>%
      layout(title = 'Revenue',
      xaxis = list(title = "Date",
      showgrid = FALSE),
      yaxis = list(title = "Revenue",
      showgrid = FALSE))









      share|improve this question














      I have a peice of code that produces a plotly graph but the issue is that each time I run this code not all the columns are necessarily there. At the minute I am just running the code till I get an error and then commenting out the add_trace part that is missing but this is getting tedious.



      How can I set it up to look at the df and add the traces for only the columns that currently exist.



       graph <- plot_ly(Data, x = ~Weeknumber, y = ~Student, name = 'Student', type = 'scatter', mode = 'none', fill = 'tozeroy', fillcolor = red, hoverinfo = 'text',
      text = ~paste('Date:', WeekStart)) %>%
      add_trace(y = ~Senior, name = 'Senior', fillcolor = green) %>%
      add_trace(y = ~Child, name = 'Child', fillcolor = blue) %>%
      add_trace(y = ~Adult, name = 'Adult', fillcolor = purple) %>%
      layout(title = 'Revenue',
      xaxis = list(title = "Date",
      showgrid = FALSE),
      yaxis = list(title = "Revenue",
      showgrid = FALSE))






      r r-plotly






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 15 hours ago









      JoshJosh

      193




      193






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Reshape your Data to a long vector :



          Data <- Data %>% tidyr::gather(key="variable",value="value",-Weeknumber,-WeekStart)



          Then call plot_ly and distribute color with the variable column:



          plot_ly(Data,x=~Weeknumber, y = ~value,type = 'scatter', mode = 'none',
          fill = 'tozeroy', fillcolor = ~variable, hoverinfo = 'text',
          text = ~paste('Date:', WeekStart))






          share|improve this answer























          • Thanks but how can I make sure that each variable is using the right colour as specified?

            – Josh
            14 hours ago











          • see this paragraph : plot.ly/r/line-and-scatter/#custom-color-scales

            – Benoît Fayolle
            14 hours ago










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55021250%2floop-around-dataframe-columns-and-make-a-plotly-of-the-columns-that-are-present%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Reshape your Data to a long vector :



          Data <- Data %>% tidyr::gather(key="variable",value="value",-Weeknumber,-WeekStart)



          Then call plot_ly and distribute color with the variable column:



          plot_ly(Data,x=~Weeknumber, y = ~value,type = 'scatter', mode = 'none',
          fill = 'tozeroy', fillcolor = ~variable, hoverinfo = 'text',
          text = ~paste('Date:', WeekStart))






          share|improve this answer























          • Thanks but how can I make sure that each variable is using the right colour as specified?

            – Josh
            14 hours ago











          • see this paragraph : plot.ly/r/line-and-scatter/#custom-color-scales

            – Benoît Fayolle
            14 hours ago















          0














          Reshape your Data to a long vector :



          Data <- Data %>% tidyr::gather(key="variable",value="value",-Weeknumber,-WeekStart)



          Then call plot_ly and distribute color with the variable column:



          plot_ly(Data,x=~Weeknumber, y = ~value,type = 'scatter', mode = 'none',
          fill = 'tozeroy', fillcolor = ~variable, hoverinfo = 'text',
          text = ~paste('Date:', WeekStart))






          share|improve this answer























          • Thanks but how can I make sure that each variable is using the right colour as specified?

            – Josh
            14 hours ago











          • see this paragraph : plot.ly/r/line-and-scatter/#custom-color-scales

            – Benoît Fayolle
            14 hours ago













          0












          0








          0







          Reshape your Data to a long vector :



          Data <- Data %>% tidyr::gather(key="variable",value="value",-Weeknumber,-WeekStart)



          Then call plot_ly and distribute color with the variable column:



          plot_ly(Data,x=~Weeknumber, y = ~value,type = 'scatter', mode = 'none',
          fill = 'tozeroy', fillcolor = ~variable, hoverinfo = 'text',
          text = ~paste('Date:', WeekStart))






          share|improve this answer













          Reshape your Data to a long vector :



          Data <- Data %>% tidyr::gather(key="variable",value="value",-Weeknumber,-WeekStart)



          Then call plot_ly and distribute color with the variable column:



          plot_ly(Data,x=~Weeknumber, y = ~value,type = 'scatter', mode = 'none',
          fill = 'tozeroy', fillcolor = ~variable, hoverinfo = 'text',
          text = ~paste('Date:', WeekStart))







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 15 hours ago









          Benoît FayolleBenoît Fayolle

          1313




          1313












          • Thanks but how can I make sure that each variable is using the right colour as specified?

            – Josh
            14 hours ago











          • see this paragraph : plot.ly/r/line-and-scatter/#custom-color-scales

            – Benoît Fayolle
            14 hours ago

















          • Thanks but how can I make sure that each variable is using the right colour as specified?

            – Josh
            14 hours ago











          • see this paragraph : plot.ly/r/line-and-scatter/#custom-color-scales

            – Benoît Fayolle
            14 hours ago
















          Thanks but how can I make sure that each variable is using the right colour as specified?

          – Josh
          14 hours ago





          Thanks but how can I make sure that each variable is using the right colour as specified?

          – Josh
          14 hours ago













          see this paragraph : plot.ly/r/line-and-scatter/#custom-color-scales

          – Benoît Fayolle
          14 hours ago





          see this paragraph : plot.ly/r/line-and-scatter/#custom-color-scales

          – Benoît Fayolle
          14 hours ago



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55021250%2floop-around-dataframe-columns-and-make-a-plotly-of-the-columns-that-are-present%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

          Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

          Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved