简体   繁体   English

独立于 plotly r 中的实际跟踪数据更改图例跟踪颜色

[英]Change legend trace color independently of actual trace data in plotly r

How do I change the color of the marker in a plotly legend, independently of what is shown?如何更改 plotly 图例中标记的颜色,与显示的内容无关?

library(plotly)
library(data.table)
data=data.table(x=seq(1,100), y=rnorm(100), group = sample(letters[1:3], ,size=100, replace=T))
plot_ly(type="scatter", mode="markers") %>% 
  add_trace(data=data[group=="a"], x=~x,y=~y, name="Metric", legendgroup="m", showlegend=T, marker=list(size=10,color="red")) %>% 
  add_trace(data=data[group=="b"], x=~x,y=~y, name="Metric2", legendgroup="m", showlegend=F, marker=list(size=10,color="blue")) %>% 
  add_trace(data=data[group=="c"], x=~x,y=~y, name="Metric3", legendgroup="m", showlegend=F, marker=list(size=10,color="green")) %>% 
  layout(showlegend=T)

I want the single red circle in the legend to be black, instead of red, while I want the points on the plot to be red/blue/green as they currently are.我希望图例中的单个红色圆圈为黑色,而不是红色,而我希望 plot 上的点为当前的红色/蓝色/绿色。

示例图

One way of getting this effect is by including add_markers() with visible = 'legendonly' and setting showlegend = F for all your add_trace() lines:获得这种效果的一种方法是包含add_markers()visible = 'legendonly'并为所有add_trace()行设置showlegend = F

library(plotly)
library(data.table)
data=data.table(x=seq(1,100), y=rnorm(100), group = sample(letters[1:3], ,size=100, replace=T))
plot_ly(type="scatter", mode="markers") %>% 
  add_trace(data=data[group=="a"], x=~x,y=~y, name="Metric", legendgroup="m", showlegend=F, marker=list(size=10,color="red")) %>% 
  add_trace(data=data[group=="b"], x=~x,y=~y, name="Metric2", legendgroup="m", showlegend=F, marker=list(size=10,color="blue")) %>% 
  add_trace(data=data[group=="c"], x=~x,y=~y, name="Metric3", legendgroup="m", showlegend=F, marker=list(size=10,color="green")) %>%
  add_markers(x = 1, y = 1, name = 'Metric', visible = 'legendonly', showlegend = T, marker = list(size = 10, color = 'black')) %>% 
  layout(showlegend=T)

which gives这使在此处输入图像描述

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

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