3

I am trying to map color over two variables using Restyle Buttons of keeping the y and x-axis dynamics when changing the colours. When I add color=~fruit in the main plot it gives the result I am looking for, but I lose the dynamics of the axes when changing the variables. I basically want to change the color of the lines referent to the fruits. Below are data and the code I am using for playing with that. Thanks for any help or hints!

libraries

library(dplyr); library(plotly);

data

dfake <- tibble(days = seq(1,100, by=1),
                 bask = seq(1,500, by=5),
                fruit = c(rep("grape", 50), 
                          rep("apple", 50)));

plotting code

plot <- dfake %>%
        plot_ly(x = ~days, y = ~bask, text = ~fruit, 
                type = 'scatter', 
                mode = 'lines', 
                hoverinfo = 'text',
                transforms = list(
                        list(type = 'filter',
                             target = ~fruit,
                             operation = '=',
                             value = unique(dfake$fruit)[1]))) %>%
        layout(updatemenus = list(
                list(type = 'dropdown',
                     active = 1,
                     buttons = list(
                             list(method = "restyle",
                                  args = list("transforms[0].value", 
                                              unique(dfake$fruit)[1]),
                                  label = unique(dfake$fruit)[1]),
                             list(method = "restyle",
                                  args = list("transforms[0].value", 
                                              unique(dfake$fruit)[2]),
                                  label = unique(dfake$fruit)[2])))));

plot;

2 Answers 2

3

Yes, wasn't sure of the data was in the best possible format. So, I was fiddling with this in the following manner:

  1. Make a , where each Y-axis variable goes into individual column (refer to the tidyverse philosopy).
  2. Appending the lines layer-by-layer into .
  3. Using updatemenus to get the interactive buttons & desired visibility.
#Converting into a dataframe, mutating new columns for each fruit and getting their name:
df_dfake <- as.data.frame(dfake)
df_dfake <- df_dfake %>% mutate(fruit1_bask = case_when(fruit == "grape" ~ bask),
                                fruit2_bask = case_when(fruit == "apple" ~ bask))
fruit1 <- unique(dfake$fruit)[1]; fruit2 <- unique(dfake$fruit)[2];

#Plotly, adding layer by layer:
fig <- df_dfake %>% plot_ly(type = 'scatter', 
                            mode = 'lines', 
                            hoverinfo = 'text');
fig1 <- fig %>% add_lines(x = ~days , y = ~fruit1_bask, text = ~fruit,
                  line=list(color="#33CFA5"));
fig2 <- fig1 %>% add_lines(x = ~days, y = ~fruit2_bask, text = ~fruit,
                         line=list(color="#F06A6A")); 
fig2;  

fig2

enter image description here

Now, updatemenus component, to make the interactive buttons

updatemenus <- list(
  list(
    active = -1,
    type= 'buttons',
    buttons = list(
      list(
        label = unique(dfake$fruit)[1],
        method = "update",
        args = list(list(visible = c(FALSE, TRUE)),# this defines visibility on click
                    list(title = "fruit1",
                         annotations = list(c(), df_dfake$fruit1_bask)))),
      list(
        label = unique(dfake$fruit)[2],
        method = "update",
        args = list(list(visible = c(T, F)),# this defines visibility on click
                    list(title = "fruit2",
                         annotations = list(c(), df_dfake$fruit2_bask))))
      )
  )
)

fig3 <- fig2 %>% layout(title = "Apples & Oranges", showlegend=FALSE,
                      xaxis=list(title="Days"),
                      yaxis=list(title="Basket"),
                      updatemenus=updatemenus); fig

Which results in the following graphs with interactive buttons:

fig3 enter image description here enter image description here enter image description here

Check Update Button to learn more :)

Sign up to request clarification or add additional context in comments.

2 Comments

Very intelligent the way you worked it out! Thanks a lot! I am not wondering if it is possible to add another button to visualize the yaxis in liner or log scale. Do you have a hint how to do that? I am amazed by the functional of plotly! A fantastic way to present results.
Thanks to the developer :D Yeah, why not... try it! Go to the link I shared (it has nice examples). Maybe post another question if problem arise, so that this post is not cluttered with too many things :)
1

I finally got what I was looking for, very easy actually, I just needed to have my grape and apple not inside column fruit but as different columns and treat them with add_trace. Setting one as invisible. In each add_trace I was free to play with color, width etc. After this organisation it was easier to work with button . I hope this simple coding can help someone. If not sorry.

dfake <- tibble(days = seq(1,100, by=1),
                grape = seq(1,500, by=5),
                apple = seq(501,1000, by=5))
fig <- plot_ly(dfake, x = ~days) %>%
        add_trace(y = ~grape, name = 'Bask',
                  mode = 'lines+markers', type = "scatter",
                  marker = list(color = 'blue'),
                  line = list(color = 'blue', width = 4)) %>%
        add_trace(y = ~apple, name = 'New', visible = F,
                  mode = 'lines+markers', type = "scatter",
                  marker = list(color = 'red'),
                  line = list(color = 'red', width = 4)) %>%
        layout(
                title = "Corona global Cases and Depths",
                xaxis = list(domain = c(0.1)),
                yaxis = list(title = "yaxis"),
                updatemenus = list(
                        list(y = 0.9,
                             buttons = list(
                                     list(method = "restyle",
                                          args = list("visible", list(TRUE, FALSE)),
                                          label = "Grape"),
                                     list(method = "restyle",
                                          args = list("visible", list(FALSE, TRUE)),
                                          label = "Apple")))))
fig

1 Comment

Yes, exactly. Sometimes getting the data-wrangling is the key to get good output, easily.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.