1

This should be easy to do, but the solution eludes me. How do I remove the legend in a contour plot made with plotly for R? This doesn't work for me.

# Reproducible example
p <- plot_ly(mtcars, x = ~wt, y = ~cyl, z = ~mpg, type = "contour")
hide_legend(p) # Doesn't work
p %>% layout(showlegend = FALSE) # Doesn't work
plot_ly(mtcars, x = ~wt, y = ~cyl, z = ~mpg, type = "contour", showlegend = FALSE) # Doesn't work

Actually, what I really want to do is to have a single legend for multiple contour plots in the same figure, but assigning them to the same legendgroup doesn't work either, so I'd rather label the contours and get rid of all the legends than have a dozen legends cluttering my plot:

plot_ly() %>% 
  add_trace(..., type = "contour", ...) %>% # This adds a legend
  add_trace(..., type = "contour", ...) %>% # This adds another unwanted legend
  add_trace(..., type = "contour", ...) %>% # This adds yet another unwanted legend
  ...
  add_trace(..., type = contour", ...) # Now I have dozens of legends that I don't want!

I'm using Plotly for R, not Plotly for Python.

2
  • I see you've accepted an answer regarding the legend. Are you still interested in the second part of your question?
    – vestland
    Commented Feb 8, 2021 at 16:24
  • Actually, your answer was enough for me to figure out the rest of what I wanted to do. You enabled me to make progress on the 2nd part. Thanks! 😊 (FYI, what I'm doing is overlaying several plots and controlling their visibility with a slider, and I wanted a common colorbar instead of one for each plot. This works for me now.) Commented Feb 9, 2021 at 14:28

1 Answer 1

2

The legend can be hidden like:

p <- plot_ly(mtcars, x = ~wt, y = ~cyl, z = ~mpg, type = "contour")
p %>% hide_colorbar()
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.