简体   繁体   中英

ggplotly removes legend from ggplot

ggplotly removes the legend of a geom_line plot using ggplot.

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

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()

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM