plotly.R icon indicating copy to clipboard operation
plotly.R copied to clipboard

Add support for geom_boxplot(stat="identity") in ggplotly or similar for plot_ly boxplot

Open mj-2016 opened this issue 8 years ago • 8 comments

I'm using ggplot2 to create boxplots which I'd then like to make interactive by converting to a plotly object. What I need to be able to do however is use my own computed values for the min, max, lower, upper, and middle values of the boxplot. This is possible in ggplot by using stat="identity" in geom_boxplot() and setting the values within aes(). When I then pass the ggplot object to ggplotly however nothing renders, I just get an empty axis. I think this is because plotly tries to internally calculate the quartiles etc when converting the box plot (which it then can't do because I've not given it appropriate data).

Would it therefore be possible to add support for setting the boxplot values either through the ggplotly conversion or directly using a plot_ly() boxplot?

Thanks

p.s. a simple example of what I'd like to convert can be found at the end of the geom_boxplot documentation (http://docs.ggplot2.org/current/geom_boxplot.html).

mj-2016 avatar Apr 28 '16 09:04 mj-2016

This won't be possible until this gets addressed https://github.com/plotly/plotly.js/issues/242

cpsievert avatar Apr 28 '16 11:04 cpsievert

Now that the above issue has been fixed in plotly/plotly.js#4432, can we please reconsider this?

joshuablake avatar May 07 '20 09:05 joshuablake

It's currently possible via add_trace():

plot_ly() %>% 
  add_trace(lowerfence = 0, q1 = 1, median = 3, q3 = 5, upperfence = 10, type = "box")

Reopening though since add_boxplot() probably shouldn't error if you provide q1/median/q3 instead of x/y

cpsievert avatar May 07 '20 14:05 cpsievert

ggplotly appears to also have issues with this. The following gives a boxplot with no data.

y <- rnorm(100)
(data.frame(
  x = 1,
  y0 = min(y),
  y25 = quantile(y, 0.25),
  y50 = median(y),
  y75 = quantile(y, 0.75),
  y100 = max(y)
) %>%
  ggplot(aes(x)) +
  geom_boxplot(
    aes(ymin = y0, lower = y25, middle = y50, upper = y75, ymax = y100),
    stat = "identity"
  )) %>%
  plotly::ggplotly()

joshuablake avatar May 08 '20 08:05 joshuablake

Is this issue going to be solved? I'm still having the same issue as @joshuablake on a similar code.

TucoFernandes avatar Oct 20 '21 14:10 TucoFernandes

+1. I'm having the same issue as @joshuablake and @TucoFernandes. Please see question at https://stackoverflow.com/questions/75349810/using-plotly-with-ggpolot2-geom-plot-with-stat-identity-results-in-empty-canvas. Btw, this (using pre-computed quartile values in geom_boxplot) is necessary because plotly is unable to handle large datasests (in my case ~46 million rows). Thank you.

mpetruc avatar Feb 05 '23 21:02 mpetruc

Anyone know if there's any update on this issue? I have the exact same issue as @joshuablake. I have to use precomputed values because of size and for validation purposes.

smritia avatar Feb 14 '23 09:02 smritia

I'm having the exact same issue as @smritia @joshuablake

judytlewis avatar Feb 14 '24 18:02 judytlewis