0

I would like to make box plot using crosstalk, ggplot2 and plotly. But I encountered 2 issues:

  1. the fill color is lost in the box plot

  2. I cannot remove the All option in the dropdown menu.

Below is my demo code.

Thanks a lot for your help.

sd = SharedData$new(mpg)

# Create a filter input
continent_filter = filter_select(id = "Year", label = "Year", sharedData = sd, group = ~ year, multiple = F)

# Render graph
bscols(
  continent_filter, 
  ggplotly(
    ggplot(aes(y = hwy, x = manufacturer, group = year), data = sd) + 
      geom_boxplot(aes(fill = manufacturer)), tooltip = NULL
  ),
  widths = 12
)
Wang
  • 1,314
  • 14
  • 21

1 Answers1

1

The issue with your lost fill is that you mapped year on the group aes in ggplot. This can seen by just plotting the ggplot which also has no fill:

library(ggplot2)

ggplot(aes(y = hwy, x = manufacturer, group = year), data = mpg) + 
  geom_boxplot(aes(fill = manufacturer))

To fix that map the interaction of year and manufacturer on the group aes:

library(crosstalk)
library(plotly)

sd = SharedData$new(mpg)

# Create a filter input
continent_filter = filter_select(id = "Year", label = "Year", sharedData = sd, group = ~ year, multiple = F)

# Render graph
bscols(
  continent_filter, 
  ggplotly(
    ggplot(aes(y = hwy, x = manufacturer, group = interaction(year, manufacturer)), data = sd) + 
      geom_boxplot(aes(fill = manufacturer)), tooltip = NULL
  ),
  widths = 12
)

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51