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

ggplotly does not display geom_errorbar legend keys

Open davidhodge931 opened this issue 3 years ago • 1 comments

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)

image

davidhodge931 avatar Dec 23 '21 21:12 davidhodge931

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)

tefavidal avatar Mar 11 '22 16:03 tefavidal