I am making a function that creates a plotly raincloud plot for any given dataset through the ggplotly function. Most of it was very straightforward but the boxplot overlay is not working.
My current best try is this:
raincloud <- ggplot2::ggplot(data = iris) +
ggplot2::geom_density(mapping = ggplot2::aes(x = Sepal.Length,
fill = Species,
colour = NA),
alpha = .85) +
ggplot2::geom_rug(mapping = ggplot2::aes(x = Sepal.Length,
colour = Species),
size = 1,
alpha = .6) +
ggplot2::geom_boxplot(mapping = ggplot2::aes(x = Sepal.Length,
y = -.1,
fill = Species,
colour = Species),
width = 0.2,
alpha = .3) +
ggplot2::facet_grid(rows = 'Species') +
ggplot2::theme_minimal()
raincloud
plotly::ggplotly(raincloud)
This approach has two main issues:
- The boxplot is artificially placed inside the plot area, not on the margin. And because I set it at fixed
y
and fixedwidth
, it can either overlap with the density or look minuscule below it, depending on the height of the distribution. This is why I concluded that I need to place it in the plot margin, over the geom_rug.
- When generating the ggplotly, the box plot just disappears and leaves only a dotted line, as if it was another geom_rug. I know plotly has boxplot support, but perhaps the combination of horizontal + faceted is too much to ask for its translator.
I insist on the emphasis of needing it to be plotly-compatible, so custom geoms from ggdist
, ggExtra
or the like are not an option since they are not supported by ggplotly's translator.