Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplotly removes legend from ggplot

Tags:

r

ggplot2

plotly

ggplotly removes the legend of a geom_line plot using ggplot.

See e.g. below:

library(plotly)    
g <- ggplot(iris)
g = g + geom_line(aes(x = Sepal.Length, y = Sepal.Width, color = Species), size = 0.05)
g # Here is a legend
(gg <- ggplotly(g)) # Legend has now been removed.

Any ideas how to get back the legend?

I am using plotly_2.0.19 and ggplot2_2.0.0.9000.

like image 591
Nick Avatar asked Jan 05 '16 12:01

Nick


1 Answers

For some reason ggplotly never adds a legend for geom_line. The documentation only has legends when points are also added. I suggest using transparent points as a work around.

{ ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_line() +
  geom_point(alpha = 0) } %>%
  ggplotly()

enter image description here

like image 151
Axeman Avatar answered Oct 07 '22 16:10

Axeman