[英]R Shiny + plotly : change color of a trace with javascript without affecting markers and legend in multiple plots
[英]Change legend trace color independently of actual trace data in plotly r
如何更改 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)
我希望圖例中的單個紅色圓圈為黑色,而不是紅色,而我希望 plot 上的點為當前的紅色/藍色/綠色。
獲得這種效果的一種方法是包含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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.