plotly.R
plotly.R copied to clipboard
ggplotly does not display geom_errorbar legend keys
It drops off the keys in the legend.
library(dplyr)
library(ggplot2)
library(palmerpenguins)
plot_data <- penguins %>%
group_by(sex, species) %>%
summarise(middle = median(body_mass_g, na.rm = TRUE),
lower = quantile(body_mass_g, probs = 0.25, na.rm = TRUE),
upper = quantile(body_mass_g, probs = 0.75, na.rm = TRUE))
p <- ggplot(plot_data) +
geom_errorbar(aes(x = sex, ymin = lower, ymax = upper, col = species))
plotly::ggplotly(p)
This is not a solution, but a work around is to add a geom_point
layer that will get shown in the legend. The order is important, the errorbar needs to be the last layer.
p <- ggplot(plot_data,aes(x = sex, y = (lower + upper)*0.5)) + geom_point(aes(color = species))
p <- p + geom_errorbar(aes(x = sex, ymin = lower, ymax = upper, col = species))
plotly::ggplotly(p)