简体   繁体   中英

Remove legend(s) from contour plots in plotly for R

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.

The legend can be hidden like:

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

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