Separate symbol and color in plotly legend

Viewed 4840

I want to achieve the same result as this ggplot code with plotly:

mtcars %>% add_rownames('car') %>% 
ggplot(aes(x = mpg,
         y = disp,
         color = as.factor(gear),
         shape = as.factor(cyl))) +
geom_point()

which results in: ggplot version

My plotly code is:

library(dplyr)
mtcars %>% add_rownames('car') %>% 
plot_ly(x = ~mpg, 
      y = ~disp,
      text = ~car,
      color = ~as.factor(gear),
      symbol = ~as.factor(cyl),
      mode = 'markers')

which enumerates all possible combinations of colors and shapes in the legend. enter image description here

Is there a way to have a similar legend to the ggplot?

1 Answers
Related