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

ggplotly only displays one legend

Open tomwagstaff-opml opened this issue 2 years ago • 1 comments

While ggplot produces legends for each aesthetic (other than x & y), after calling ggplotly on the result, only one legend remains (although both titles display).

library(tidyverse)
library(gapminder)
library(plotly)

p <- gapminder %>%
  filter(year == 2007) %>%
  mutate(pop_m = pop / 1000000) %>%
  ggplot(aes(x = gdpPercap, y = lifeExp, colour = continent, size = pop_m, text = country)) +
  geom_point() +
  scale_x_log10(label = scales::comma) +
  scale_size_continuous(label = scales::comma, breaks = c(0, 1, 10, 100, 1000), range = c(1, 10)) +
  labs(x = "GDP per capita ($)", y = "Life expectancy (years)", size = "Population, millions", colour = "Continent")

p

ggplotly(p, tooltip = "text")

ggplot_output ggplotly_output

I don't think this "disappearing legend" problem is a duplicate amongst the issues I've reviewed, but there is some discussion of this on StackOverflow.

tomwagstaff-opml avatar Apr 14 '23 15:04 tomwagstaff-opml

I guess this is a duplicate of #2163, but ultimately it's because scatter plots in plotly just don't provide size legends (#705), which renders size encodings functionally useless.

j-andrews7 avatar Jul 06 '24 20:07 j-andrews7