从 R 的 plotly 等高线图中删除图例

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

这应该很容易做到,但我找不到解决方案。如何删除用 plotly for R 制作的等高线图中的图例?这对我不起作用。

# 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

实际上,我真正想要做的是在同一个图中为多个等高线图设置一个图例,但是将它们分配到同一个图例组也不起作用, 所以我宁愿标记等高线并摆脱所有的图例,也不愿让一打图例使我的情节混乱:

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!

我正在为 R 使用 Plotly,而不是为 Python 使用 Plotly。

图例可以像这样隐藏:

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