mapping color over variables using Restyle Buttons of plotly using R

Viewed 747

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

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 :)

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
Related