繁体   English   中英

如何使图例出现在情节上但不绘制相关点

[英]How to make a legend appear on plotly but not plot the associated points

我有一个情节,我只想渲染图例,但不想渲染任何情节点。 谁能告诉我如何做到这一点?

library(tidyverse)
library(plotly)

mtcars %>% 
  mutate(cyl = factor(cyl)) %>% 
  plot_ly() %>% 
  add_markers(x = ~mpg,
              y = ~hp,
              color = ~cyl,
              colors = c("4" = "red", "6" = "green", "8" = "blue"))

目前我有这个:

在此处输入图片说明

我想要的是这个:

在此处输入图片说明

不确定这个结果应该有什么好处,但你可以像这样禁用legendclick:

编辑:也可以没有onRender

library(plotly)
mtcars %>% 
  mutate(cyl = factor(cyl)) %>% 
  plot_ly() %>% 
  add_markers(x = ~mpg,
              y = ~hp,
              color = ~cyl,
              colors = c("4" = "red", "6" = "green", "8" = "blue"), visible='legendonly') %>%
  layout(legend=list(itemclick = FALSE, itemdoubleclick = FALSE)) 

library(htmlwidgets)
library(plotly)

js <- c(
  "function(el, x){",
  "  el.on('plotly_legendclick', function() {",
  "    return false;",
  "  });",
  "  el.on('plotly_legenddoubleclick', function() {",
  "    return false;",
  "  });",
  "}")

mtcars %>% 
  mutate(cyl = factor(cyl)) %>% 
  plot_ly() %>% 
  add_markers(x = ~mpg,
              y = ~hp,
              color = ~cyl,
              colors = c("4" = "red", "6" = "green", "8" = "blue"), visible='legendonly') %>%
  onRender(js)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM