英文:
Group legend and colors plotly R
问题
基于下面的可重现示例,我想让 Variable1
在图中具有相同的颜色,而不是两种颜色,Variable2
也是如此。然后,图例只需要显示2条线:每种颜色各一条,而不是现在的4条。
如何实现这一点?
library(plotly)
df <- data.frame(Date = c(seq.Date(as.Date("2022-01-01"),
as.Date("2022-06-30"),
by = "day"),
seq.Date(as.Date("2023-01-01"),
as.Date("2023-06-30"),
by = "day")),
Period = rep(c(1,2), each = 181),
Variable1 = runif(362),
Variable2 = c(rep(0.4, 181), rep(0.6, 181)))
plot_ly(data = df, x = ~Date, y = ~Variable1, type = "scatter", mode = "lines", name = "Variable 1", split = ~Period) %>%
add_trace(x = ~Date, y = ~Variable2, name = "Variable 2", split = ~Period)
英文:
Based on the reproducible example below, I would like Variable1
to have the same color on the plot instead of 2, and same for Variable2
. Then, the legend would need to show 2 lines only : one for each color, instead of 4 for now.
How can I achieve this ?
library(plotly)
df <- data.frame(Date = c(seq.Date(as.Date("2022-01-01"),
as.Date("2022-06-30"),
by = "day"),
seq.Date(as.Date("2023-01-01"),
as.Date("2023-06-30"),
by = "day")),
Period = rep(c(1,2), each = 181),
Variable1 = runif(362),
Variable2 = c(rep(0.4, 181), rep(0.6, 181)))
plot_ly(data = df, x = ~Date, y = ~Variable1, type = "scatter", mode = "lines", name = "Variable 1", split = ~Period) %>%
add_trace(x = ~Date, y = ~Variable2, name = "Variable 2", split = ~Period)
答案1
得分: 1
要为两个迹线设置相同颜色,您可以使用参数color
,以及函数I()
,例如color = I('#1f77b4')
。
因为您使用了split
,您创建了一个新的迹线,这就是为什么您看到了四个图例条目的原因。如果您想让这两个期间只有一个图例条目,您可以使用legendgroup
将它们分组,您可以分配一个您喜欢的字符名称(这是您在图例中看到的内容)。
为了确保每个组只显示一个图例项,您需要更改每个图例组的showlegend
参数。因为在单个迹线的调用中输入多个showlegend
没有直接的方法,我创建了fixer()
。这个函数所做的就是将第2个和第4个迹线的showlegend
改为F
。
fixer <- function(plt) { # 更改4条迹线中的2条图例
plt <- plotly_build(plt)
plt$x$data[[2]]$showlegend <- plt$x$data[[4]]$showlegend <- F
plt
}
plot_ly(data = df, x = ~Date, y = ~Variable1, type = "scatter", mode = "lines",
name = "Variable 1", split = ~Period,
color = I('#1f77b4'), legendgroup = "Variable 1") %>% # <<--- 我是新的!
add_trace(x = ~Date, y = ~Variable2, name = "Variable 2", split = ~Period,
color = I('#ff7f0e'), legendgroup = "Variable 2") %>% # <<--- 我是新的!
fixer()
英文:
To set one color for two traces, you can use the argument color
, along with the function I()
, such as color = I('#1f77b4')
.
Because you used split
, you created a new trace, which is why you see four legend entries. If you want the two periods two have one legend entry, you can group them with legendgroup
, where you can assign and character name you would like (this is what you'll see in the legend).
To make sure only one legend item is shown per group, you need to change the parameter showlegend
for one of each legend group. Because there isn't a straightforward method of entering multiple showlegend
in a single call for a trace, I've created fixer()
. All this function does is change the 2d and 4th trace to showlegend = F
.
fixer <- function(plt) { # change legend for 2 of 4 traces
plt <- plotly_build(plt)
plt$x$data[[2]]$showlegend <- plt$x$data[[4]]$showlegend <- F
plt
}
plot_ly(data = df, x = ~Date, y = ~Variable1, type = "scatter", mode = "lines",
name = "Variable 1", split = ~Period,
color = I('#1f77b4'), legendgroup = "Variable 1") %>% # <<--- I'm new!
add_trace(x = ~Date, y = ~Variable2, name = "Variable 2", split = ~Period,
color = I('#ff7f0e'), legendgroup = "Variable 2") %>% # <<--- I'm new!
fixer()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论