JavRoNu
JavRoNu

Reputation: 368

Using R plotly dropdown menu to select variable and keep ussing the colour variable as a trace

I was adding a variable selector to a plotly graph in R (following the usual approach that consist in hiding the unnecessary traces from the plot)

library(plotly)

dat <- mtcars
dat$cyl <- factor(dat$cyl)
dat$car <- rownames(mtcars)

dat %>% 
    plot_ly(x = ~car, y = ~mpg,
            name='mpg', type='scatter', mode='markers') %>%
     add_trace(y = ~hp, name = 'hp', type='scatter', mode='markers') %>%
     add_trace(y = ~qsec, name = 'qsec', type='scatter', mode='markers') %>%
       layout(
          updatemenus = list(
            list(
                type = "list",
                label = 'Category',
                buttons = list(
          list(method = "restyle",
               args = list('visible', c(TRUE, FALSE, FALSE)),
               label = "mpg"),
          list(method = "restyle",
               args = list('visible', c(FALSE, TRUE, FALSE)),
               label = "hp"),
          list(method = "restyle",
               args = list('visible', c(FALSE, FALSE, TRUE)),
               label = "qsec")
        )
      )
    )
  )

enter image description here

The code does the job BUT it overrides the standard use of a group/color variable as a trace selector because we already set up the traces.

The standard use of a color/group variable will be something like this

plot_ly(group_by(dat,cyl), x = ~car, y = ~mpg, color = ~cyl, type='scatter', mode='markers')

NOTE: I use group_by() becuse group = ... is deprecated.

enter image description here

Is possible to add the dropdown menu as a variable selector and still been able to use a color/group variable to hide & show the data of the selected variable?

I tried adding color= ~cyl but, as you can imagine, it doesn’t work.

Thanks in advance!!

Upvotes: 2

Views: 3060

Answers (1)

stefan
stefan

Reputation: 124283

Try this. First, I converted the dataset to long format so that the variables become categories of one variable name with values in value. Second, I map name on symbol and cyl on color. Third. I adjust the legend labels so that only cyl shows up in the legend by mapping cyl on name inside add_trace.

library(plotly)

dat <- mtcars
dat$cyl <- factor(dat$cyl)
dat$car <- rownames(mtcars)

dat %>% 
  tidyr::pivot_longer(c(mpg, hp, qsec)) %>% 
  plot_ly(x = ~car, y = ~value, color = ~cyl, symbol = ~name) %>%
  add_trace(type='scatter', mode='markers', name = ~cyl) %>% 
  layout(
    updatemenus = list(
      list(
        type = "list",
        label = 'Category',
        buttons = list(
          list(method = "restyle",
               args = list('visible', c(TRUE, FALSE, FALSE)),
               label = "hp"),
          list(method = "restyle",
               args = list('visible', c(FALSE, TRUE, FALSE)),
               label = "mpg"),
          list(method = "restyle",
               args = list('visible', c(FALSE, FALSE, TRUE)),
               label = "qsec")
        )
      )
    )
  )

enter image description here

Upvotes: 5

Related Questions