I have been using the wonderful ggplot
library in R but love the interactivity of plotly
. For most of the plots I want, ggplotly()
does what I need, but for a few key ones, it doesn't. The plot is pretty straightforward in ggplot
.
Here is some sample data alongside the plotting code:
sample = crossing(x = 1:10,
g1 = c("A", "B", "C"),
g2 = c("a", "b", "c", "d"),
c = 1:5) %>%
mutate(mid = runif(600),
low = mid * 0.8,
hi = mid * 1.2)
p1 = sample %>%
ggplot(aes(x = x, colour = as_factor(c))) +
geom_line(aes(y = mid)) +
geom_ribbon(aes(ymin = low, ymax = hi, fill = as_factor(c)), alpha = 0.2) +
facet_grid(rows = vars(g1), cols = vars(g2))
p1 %>% ggplotly()
doesn't handle the legend that well. The ribbon fill is attached to all subplots but the line colour is matched to each of the 15 subplots individually. And they are broken into separate legend items.
I almost get there if I just remove the colour aesthetic from p1
, but then all the ribbons are gray. Is there an easy way to modify this for ggplotly
? I'd also be open to doing this natively in plotly
, but it looks like the subplot
function requires much more coding to get it to work.
Thanks for any advice.