Filtering histograms using Crosstalk and Plotly without using the `group` argument

This issue is a cross-post from Stack Overflow here. I haven't received any responses in over a week, so I'm hoping that someone from the RStudio Community may have some insights.

I'm creating a project where I'm hoping to embed an interactive histogram into an html page generated from an R Markdown document. Because of the costs associated with the number of interactive histograms and the potential volume of usage, Shiny is not a feasible approach for this project, so I'm attempting to embed an html widget for an interactive histogram that can be filtered using Crosstalk and Plotly. So far, I've been able to create a filterable histogram when using the group argument in ggplot(aes()) (reprex included below). Ultimately, I would like to use multiple variables to filter the histogram, which would cause problems if I'm using the group argument.

Is there a way that I can filter histograms using Crosstalk and Plotly without using the group argument?

Thanks!

library(crosstalk)
library(plotly)
library(reprex)

shared_mtcars <- SharedData$new(mtcars)
bscols(widths = c(3, NA),
       list(
         filter_checkbox("cyl", "Cylinders", shared_mtcars, ~cyl, inline = TRUE)
       ),
       plotly::ggplotly(shared_mtcars %>% 
                          ggplot(aes(x = mpg, group = factor(cyl))) + 
                          geom_histogram(fill = "pale green",
                                         color = "black") + 
                          theme(legend.position = "none"))
)

I'm not sure its a problem, you can just paste them together

library(crosstalk)
library(plotly)

shared_mtcars <- SharedData$new(mtcars)
bscols(widths = c(3, NA),
       list(
         filter_checkbox("cyl", "Cylinders", shared_mtcars, ~cyl, inline = TRUE),
         filter_checkbox("vs", "VS", shared_mtcars, ~vs, inline = TRUE)
       ),
       plotly::ggplotly(shared_mtcars %>% 
                          ggplot(aes(x = mpg,group=paste0(cyl,vs))) + 
                          geom_histogram(fill = "pale green",
                                         color = "black") + 
                          theme(legend.position = "none"))
)

Thanks @nirgrahamuk! That does resolve the issue of having two variables in the group argument.

I do have two follow-up questions. When using the group argument, horizontal bars are plotted to delineate the groups within the histogram bars. Is it possible to plot the graphs without those lines. I've included a picture to highlight which bars I'm referring to.

I'm limited to a single image per post, so I'll place my second follow-up question in another response.

Choosing which values to filter can cause some of the histogram bars to "float." I've included another picture below for reference. Is it possible to "re-center" these bars to the x-axis when filtering so that the graph looks more like a traditional histogram?

Thanks again!

the documentation for crosstalk says
Crosstalk currently only works for linked brushing and filtering of views that show individual data points, not aggregate or summary views (where “observations” is defined as a single row in a data frame). For example, histograms are not supported since each bar represents multiple data points; but scatter plot points each represent a single data point, so they are supported.

So I'm not surprised that there's undesirable behaviour given the un-catered for use case

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.