我有一个包含3个因子的散点图。我添加3条迹线,其中每条迹线对应一个因子。我希望散点图和迹线是相同的颜色。这是生成适当测试数据的简单函数:
curl -X POST \
https://jenkins.local/job/TEAM-FOLDER/credentials/store/folder/domain/_/createCredentials \
-F secret=@/Users/maksym/secret \
-F 'json={"": "4", "credentials": {"file": "secret", "id": "test",
"description": "HELLO-curl", "stapler-class":
"org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl",
"$class":
"org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl"}}'
如果我创建一个数据框并将其传递给plot_ly,如下所示:
## generate test data
getTestData <- function(seed_val=711, noise=1.0) {
set.seed(seed_val)
d <- seq(as.Date('2017/01/01'), as.Date('2017/01/08'), "days")
first_name <- rep("Jane", 8)
first_name <- append(first_name, rep("Fred", 8))
first_name <- append(first_name, rep("Sally", 8))
y1_vals <- seq(1, 3*8, 1)
y2_vals <- rnorm(3*8, mean=y1_vals, sd=noise)
dat <- data.frame(date=d, f_name=first_name, y1=y1_vals, y2=y2_vals,
stringsAsFactors = FALSE)
return(dat)
}
我明白了:
如何使散点图和相应的迹线颜色相同?
这不是我所需要的: Grouped line plots in Plotly R: how to control line color?
这非常接近: Same color assigned to same level of factor in R plotly 但是这个答案的作者指出了我在尝试各种黑客攻击时遇到的问题:
请注意,由于剧情使用的排序(有时我无法解读),因此变量因子变量可能会使这一点变得混乱。
答案 0 :(得分:2)
你在寻找这样的东西:
pal <- c("red", "blue", "green")
p1 <- plot_ly(df, x=~date, y=~y1, color=~f_name, colors = pal,
type = 'scatter', mode = "lines+markers") %>%
layout(yaxis = list(title = "some important y value")) %>%
add_trace(y=~y2, name='actual', showlegend=TRUE,
type='scatter', mode='lines',
line=list(width = 2, dash = 'dash'), color=~f_name, colors = pal)
p1