add_trace:控制颜色 [英] add_trace: control the color
问题描述
我有一个情节,在这里我有第一个灰色的迹线,该迹线会被其他颜色的迹线覆盖.我的问题是在plotly
版本4.7.1中.以及版本4.8.0.我无法调整颜色.
I have a plot where I have first trace in gray that will be overplotted by other traces in colors. My issue is that in plotly
-version 4.7.1. as well as in version 4.8.0. I can't adjust the color.
一年前,此代码有效:
mysim=data.frame(x=rep(1:4,4),y=rbinom(16,10,0.5),id=rep(1:4,each=4))
my_colors<-c( ## add the standard plotly colors
'#1f77b4', #// muted blue
'#ff7f0e', #// safety orange
'#2ca02c', #// cooked asparagus green
'#d62728' #// brick red
)
plot_ly() %>%
add_trace(x=1:4,y=rbinom(4,10,0.4),type='scatter',mode='lines',
line=list(color='#CCCCCC',dash='dashed'),hoverinfo='skip',opacity=0.25) %>%
add_trace(data=mysim,x=~x,y=~y,type='scatter',mode='lines', split=~as.factor(id),
line=list(color=my_colors),hoverinfo='skip',opacity=1)
可悲的是,我没有那台机器了.但此后似乎对plotly
进行了更改.我还尝试使用color
参数而不是split
,并使用colors
而不是line
-list来指定颜色.它没有任何影响.我仍然得到这个图:
Sadly I do not have that machine anymore. But it seems that there were changes made to plotly
since then. I also tried using the color
argument instead of split
and used colors
instead of the line
-list to specify the colors. It didn't have any impact. I still get this plot:
我在这里想念什么?我该如何运作?
What am I missing here? How can I make it work?
推荐答案
请参见此问题一个>.
如果使用color
而不是split
,并且在开始时在plot_ly
函数中使用参数colors
设置颜色,则此方法有效:
This works if you use color
instead of split
and if you set the colors in the plot_ly
function at the beginning, with the argument colors
:
plot_ly(colors=my_colors) %>%
add_trace(x=1:4,y=rbinom(4,10,0.4),type='scatter',mode='lines', line=list(color='rgb(0,0,255)',dash='dashed'),hoverinfo='skip',opacity=0.25) %>%
add_trace(data=mysim,x=~x,y=~y,type='scatter',mode='lines', color=~as.factor(id),
hoverinfo='skip',opacity=1)
这篇关于add_trace:控制颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!