【发布时间】:2019-03-15 10:50:27
【问题描述】:
我正在尝试找到一种方法来保持我的绘图图中线条的颜色,即使标记颜色发生变化。我将解释...一个示例数据集:
mtcars$vs<-as.factor(mtcars$vs)
mtcars$gear<-as.factor(mtcars$gear)
mtcars$Date<-sample(seq(as.Date('2016/01/01'), as.Date('2018/01/01'), by="day"), 32)
我做了一个我想要的情节:
pal <- c("red", "blue", "green")
mtcars %>%
group_by(vs) %>%
mutate(fit = fitted(loess(mpg ~ as.numeric(Date))))%>%
plot_ly(x = ~Date,
colors=pal, showlegend = T) %>%
add_markers( y = ~mpg, color= ~vs, alpha = 0.4) %>%
add_lines(y = ~fit, color = ~vs,line=list(width=4), colors=pal) %>%
layout(xaxis = list(title="Date"), title="All")
当我更改标记输入时:
mtcars %>%
group_by(vs) %>%
mutate(fit = fitted(loess(mpg ~ as.numeric(Date))))%>%
plot_ly(x = ~Date,
colors=pal, showlegend = T) %>%
add_markers( y = ~mpg, color= ~gear, alpha = 0.4) %>%
add_lines(y = ~fit, color = ~vs,line=list(width=4), colors=pal) %>%
layout(xaxis = list(title="Date"), title="All")
但随后线条颜色也会改变,我希望它们保持绿色和红色......
我尝试将“颜色”放在脚本中的不同位置。我还看到他们添加了add_trace 的帖子,我尝试了不同的方法来过滤 add_trace 中的数据,但它给出了一个错误,所以我没有以正确的方式进行操作..
谁能帮帮我?将不胜感激!
【问题讨论】: