user1420372
user1420372

Reputation: 2207

R plotly dropdown event for grouped boxplot

I would like to use a plotly dropdown event to show different grouped boxplots, however I have not been able to achieve this as yet:

The first plot shows expected output for plotly with dropdown = "4" (obtained using ggplot). The second plot is what I get...

library(tidyverse)
library(plotly)


dat <- mtcars %>%
    filter(cyl == 4 | carb == 4) %>%
    group_by(cyl, carb, am) %>%
    summarise(boxplot= list( setNames(boxplot.stats(disp)$stats,
                                      c('lower_whisker','lower_hinge','median','upper_hinge','upper_whisker')) )) %>%
    unnest_wider(boxplot) %>%
    arrange(cyl, carb, am) %>%
    ungroup() %>%
    mutate_at(vars(cyl, carb, am), as.character)

cylinders <- unique(dat$cyl)

dat %>% 
    filter(cyl == 4) %>%
    ggplot(aes(
        x = carb, 
        lower = lower_hinge, 
        upper = upper_hinge, 
        middle = median, 
        ymin = lower_whisker, 
        ymax = upper_whisker, 
        colour = am))  + 
    geom_boxplot(stat = "identity")

p <- plot_ly(type = "box")
for(icyl in cylinders){
  dataFilt <- filter(dat, cyl == icyl)
  p <- add_trace(p, 
               visible = TRUE,
               q1 = dataFilt$lower_hinge, 
               median = dataFilt$median,
               q3 = dataFilt$upper_hinge, 
               lowerfence = dataFilt$lower_whisker,
               upperfence = dataFilt$upper_whisker,
               x = dataFilt$carb,
           color = dataFilt$am,
               name=icyl
               )
}


p %>%
  layout(boxmode = "group",
      updatemenus = list(
      list(
        y = 0.8,
        buttons = list(

          list(label = cylinders[1],
               method = "update",
               args = list(list(visible = c(TRUE, FALSE, FALSE)))),
        list(label =  cylinders[2],
               method = "update",
               args = list(list(visible = c(FALSE, TRUE, FALSE)))),
          list(label =  cylinders[3],
               method = "update",
               args = list(list(visible = c(FALSE, FALSE, TRUE))))    
))))

expected_output

actual_output

Upvotes: 5

Views: 282

Answers (1)

Kat
Kat

Reputation: 18754

I wasn't able to get it to work with the loop, but I did get it to work as you had expected. Instead of using the variation where you did all the manual work to create the hinges, whiskers, etc., I used add_boxplot.

In layout you'll see that there are 6 T or F. That's because plotly doesn't save grouping as a function, it transforms the data into two separate traces. For example, the first two are cyl == "4" & am == "0" and cyl == "4" & am == "1"

cylinders <- unique(mtcars$cyl) # kept similar from original work

# added to simplify build
mtcars <- mtcars %>% mutate_at(vars(cyl, carb, am), as.character)

# made all cylinder options subplots
plot_ly() %>% 
  add_boxplot(x = ~carb, y = ~disp, color = ~am, colors = "Set2",
              data = mtcars[mtcars$cyl == cylinders[1], ],
              visible = T, inherit = F) %>%                 # visible
  add_boxplot(x = ~carb, y = ~disp, color = ~am, colors = "Set2",
              data = mtcars[mtcars$cyl == cylinders[2], ],
              visible = F, inherit = F) %>%                 # invisible
  add_boxplot(x = ~carb, y = ~disp, color = ~am, colors = "Set2",
              data = mtcars[mtcars$cyl == cylinders[3], ],
              visible = F, inherit = F) %>%                 # invisible
  layout(updatemenus = list(
    list(
      y = 0.8,
      buttons = list(
        list(label = cylinders[1],       # four cyl
             method = "restyle",
             args = list("visible", list(T, T, F, F, F, F))),
        list(label =  cylinders[2],      # six cyl
             method = "restyle",
             args = list("visible", list(F, F, T, T, F, F))),
        list(label =  cylinders[3],      # eight cyl
             method = "restyle",
             args = list("visible", list(F, F, F, F, T, T)))
      ) # end buttons
    )) # end updatedmenus list list
  ) # end layout

Here's the 4 cyl plotly and your original 4 cyl ggplot

Plotly ggplot

Upvotes: 1

Related Questions