在 plotly r 中独立于实际轨迹数据更改图例轨迹颜色
Change legend trace color independently of actual trace data in plotly r
如何独立于显示的内容更改图例中标记的颜色?
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)
我希望图例中的单个红色圆圈是黑色的,而不是红色的,而我希望绘图上的点是 red/blue/green 当前的样子。
获得此效果的一种方法是将 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_markers(x = data$x[1], y = data$y[1], name = 'Metric', visible = 'legendonly', legendgroup='m', showlegend = T, marker = list(size = 10, color = 'black')) %>%
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")) %>%
layout(showlegend=T)
这给
如何独立于显示的内容更改图例中标记的颜色?
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)
我希望图例中的单个红色圆圈是黑色的,而不是红色的,而我希望绘图上的点是 red/blue/green 当前的样子。
获得此效果的一种方法是将 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_markers(x = data$x[1], y = data$y[1], name = 'Metric', visible = 'legendonly', legendgroup='m', showlegend = T, marker = list(size = 10, color = 'black')) %>%
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")) %>%
layout(showlegend=T)
这给