How do I change the color of the marker in a plotly legend, independently of what is shown?
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.
CodePudding user response:
One way of getting this effect is by including add_markers() with visible = 'legendonly' and setting showlegend = F for all your add_trace() lines:
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)


