1

I have a dataframe that has different columns. The first one is the year, the rest are different brands. I would like to plot a graph showing how those different brands performed throughout the years in terms of profits. This graph should have a dropdown menu that allows you to choose which company you would like to see, that is a dropdown checkbox with all the brands. The checkbox should also allow you to see all of them at the same time, or just some.

#Here is my go at it.

library(plotly)
x <- seq(-2 * pi, 2 * pi, length.out = 1000)
df <- data.frame(x, y1 = sin(x), y2 = cos(x), y3=cos(2*x), y4=sin(3*x))

p <- plot_ly(df, x = ~x) %>%
  add_lines(y = ~y1, name = "Sin") %>%
  add_lines(y = ~y2, name = "Cos", visible = F) %>%
  layout(
    title = "Drop down menus - Styling",
    xaxis = list(domain = c(0.1, 1)),
    yaxis = list(title = "y"),
    updatemenus = list(

      list(
        y = 0.7,
        buttons = list(
          list(method = "restyle",
               args = list("visible", list(TRUE, FALSE)),
               label = "Sinx"),

          list(method = "restyle",
               args = list("visible", list(FALSE, TRUE)),
               label = "Cosx")))
    )
  )
p 

enter image description here In the example above, I was able to create a drop-down menu but it is not close to what I want. Also, I couldn’t grasp the answer given in this question since it uses so much html (I suck at html). Any help is greatly appreciated.

jb12n
  • 463
  • 1
  • 4
  • 18

1 Answers1

5

The question you linked uses shiny, but you do not seem to be using shiny.

Here is a non-shiny approach that is pretty close to what you want. It uses crosstalk::SharedData and crosstalk::filter_select() on long format data to give you a drop-down type input permitting multiple selections.

library(crosstalk)
library(dplyr)
library(plotly)
library(tidyr)

data_wide <- tibble(x = seq(-2 * pi, 2 * pi, length.out = 1000), 
                    y0 = 0, 
                    y1 = sin(x), 
                    y2 = cos(x), 
                    y3 = cos(2 * x), 
                    y4 = sin(3 * x))

data_long <- data_wide %>% 
  gather("series", "y", -x) %>% 
  mutate(series = case_when(series == "y0" ~ "0", 
                            series == "y1" ~ "sin(x)", 
                            series == "y2" ~ "cos(x)", 
                            series == "y3" ~ "cos(2x)", 
                            series == "y4" ~ "sin(3x)"))

data_shared <- SharedData$new(data_long, key = ~series)

p <- data_shared %>% 
  plot_ly(x = ~x, y = ~y, color = ~series) %>% 
  add_lines()

bscols(widths = c(3, NA), 
       filter_select(id = "fsid", 
                     label = "Series", 
                     sharedData = data_shared, 
                     group = ~series), 
       p)

crosstalk_filter_select

If you are not aware, clicking series in the legend of a plotly plot will toggle visibility of that series. So you may not need a drop-down menu at all and could instead just do something like:

library(plotly)

x <- seq(-2 * pi, 2 * pi, length.out = 1000)
df <- data.frame(x, y = 0, y1 = sin(x), y2 = cos(x), y3 = cos(2 * x), y4 = sin(3 * x))

plot_ly(df, type = "scatter", mode = "lines") %>% 
  add_lines(x= ~x, y= ~y,  name = "0",       line=list(color="red")) %>% 
  add_lines(x= ~x, y= ~y1, name = "sin(x)",  line=list(color="orange")) %>% 
  add_lines(x= ~x, y= ~y2, name = "cos(x)",  line=list(color="yellow")) %>% 
  add_lines(x= ~x, y= ~y3, name = "cos(2x)", line=list(color="green")) %>% 
  add_lines(x= ~x, y= ~y4, name = "sin(3x)", line=list(color="blue")) %>% 
  layout(title = "Choose Your Own Brands")

toggle visibility in legend

the-mad-statter
  • 5,650
  • 1
  • 10
  • 20