How can I hide a trace in a legend in R plotly

Viewed 6253

I have a plotly graph with several traces. Some of them I don't want to appear in the legend. How do I do this?

1 Answers

You need to set showlegend = F in your trace:

CODE

library(plotly)
plt <- plot_ly(as.data.frame(mtcars)) %>% 
  add_markers(x = ~wt, y = ~mpg, name = 'Fuel Eff.', type = 'scatter') %>% 
  add_markers(x = ~wt, y = ~hp, name = 'Power to wt. ratio', type = 'scatter', 
              showlegend = F) %>%
  layout(
    showlegend = T, 
    legend = list(orientation = 'h')
  )

Output

enter image description here

Related