plotly.R
plotly.R copied to clipboard
ggplotly only displays one legend
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")

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.
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.