组图例和颜色 plotly R

huangapple go评论86阅读模式
英文:

Group legend and colors plotly R

问题

基于下面的可重现示例,我想让 Variable1 在图中具有相同的颜色,而不是两种颜色,Variable2 也是如此。然后,图例只需要显示2条线:每种颜色各一条,而不是现在的4条。

如何实现这一点?

组图例和颜色 plotly R

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 ?

组图例和颜色 plotly R

library(plotly)

df &lt;- data.frame(Date = c(seq.Date(as.Date(&quot;2022-01-01&quot;),
                                   as.Date(&quot;2022-06-30&quot;),
                                   by = &quot;day&quot;),
                          seq.Date(as.Date(&quot;2023-01-01&quot;),
                                   as.Date(&quot;2023-06-30&quot;),
                                   by = &quot;day&quot;)),
                 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 = &quot;scatter&quot;, mode = &quot;lines&quot;, name = &quot;Variable 1&quot;, split = ~Period) %&gt;%
  add_trace(x = ~Date, y = ~Variable2, name = &quot;Variable 2&quot;, 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()

组图例和颜色 plotly R

英文:

To set one color for two traces, you can use the argument color, along with the function I(), such as color = I(&#39;#1f77b4&#39;).

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 &lt;- function(plt) {  # change legend for 2 of 4 traces
  plt &lt;- plotly_build(plt)
  plt$x$data[[2]]$showlegend &lt;- plt$x$data[[4]]$showlegend &lt;- F
  plt
}

plot_ly(data = df, x = ~Date, y = ~Variable1, type = &quot;scatter&quot;, mode = &quot;lines&quot;, 
        name = &quot;Variable 1&quot;, split = ~Period,  
        color = I(&#39;#1f77b4&#39;), legendgroup = &quot;Variable 1&quot;) %&gt;%     # &lt;&lt;--- I&#39;m new!
  add_trace(x = ~Date, y = ~Variable2, name = &quot;Variable 2&quot;, split = ~Period, 
            color = I(&#39;#ff7f0e&#39;), legendgroup = &quot;Variable 2&quot;) %&gt;% # &lt;&lt;--- I&#39;m new! 
  fixer()

组图例和颜色 plotly R

huangapple
  • 本文由 发表于 2023年7月31日 20:48:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76803792.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定