Search code examples
rggplot2plotlylegend-propertiesr-plotly

Plotly: single legend for multiple aesthetics


Is there any way to use a single legend for multiple aesthetics in Plotly? I use ggplot to plot several weeks of data and it gets very noisy. Therefore, I use both color and shape to differentiate them and ggplot just combines them in the legend. Here is a reproducible example:

p<- ggplot(mtcars, aes(mpg,wt))+
geom_point(aes(color = factor(gear), shape = factor(gear)))+
geom_line(aes(color = factor(gear)))

p

This code produces following plot: enter image description here

Now, if I use

ggplotly(p)

I get the following plot where it shows two sets of legend. Is there any way to make Plotly legend behave as ggplot's does?

enter image description here

Thanks


Solution

  • I think it might still be an issue with ggplot2 / plotly. Alternatively, you could try:

    plot_ly(data = mtcars,
            x= ~mpg, y = ~wt, color = ~factor(gear),
            type = 'scatter', mode = 'lines+markers',
            symbol = ~factor(gear),
            symbols = c('circle','triangle-up','square'))