[英]what is the sequence of palette color in dyRibbon?
Consider these questions around a reprex using dyRibbon
function of dygraphs
package in R.考虑使用 R 中dygraphs
包的dyRibbon
函数的 reprex 的这些问题。
Q1. Q1。 Why do we need 4 colors for a 3 ribbon display?为什么 3 色带显示需要 4 种颜色? Why is the first one always ignored?为什么第一个总是被忽略?
Q2. Q2。 How is the sequence of the ribbon color palette assigned to each numerical value?色带调色板的顺序是如何分配给每个数值的? It is surely not per the numerical order as you will see in the reprex below正如您将在下面的代表中看到的那样,它肯定不是按照数字顺序
library(data.table)
library(dygraphs)
library(RColorBrewer)
library(dplyr)
set.seed(12)
# create data
dt1 <- data.table(t = seq(Sys.time(),by = "1 sec", length.out = 10),
y = rnorm(10,20,5))
# add a column with 3 possible values to be used for dyRibbon
dt1[,flag1:=factor(dplyr::case_when(y>22 ~ "high",y>18 ~ "med", T ~ "low"))]
dt1[,rflagn:=as.numeric(flag1)]
# generate the dygraph with a 3 color ribbon
dyg1 <-
dt1 %>%
select(t,y) %>%
dygraph(main = "Reprex to show color sequence") %>%
dyOptions(drawPoints = T,pointSize = 5,drawGapEdgePoints = T,
strokeWidth = 4,strokeBorderWidth = 1) %>%
dyRibbon(dt1$rflagn/4,palette = brewer.pal(4,"Set1"))
# checkout the reprex graph paying attention to the sequence of colors
dyg1
# check the sequence of colors in the palette
display.brewer.pal(4,"Set1")
# check the actual values in first 10 rows
dt1
#> t y flag1 rflagn
#> 1: 2022-05-18 21:47:10 12.59716 low 2
#> 2: 2022-05-18 21:47:11 27.88585 high 1
#> 3: 2022-05-18 21:47:12 15.21628 low 2
#> 4: 2022-05-18 21:47:13 15.39997 low 2
#> 5: 2022-05-18 21:47:14 10.01179 low 2
#> 6: 2022-05-18 21:47:15 18.63852 med 3
#> 7: 2022-05-18 21:47:16 18.42326 med 3
#> 8: 2022-05-18 21:47:17 16.85872 low 2
#> 9: 2022-05-18 21:47:18 19.46768 med 3
#> 10: 2022-05-18 21:47:19 22.14007 high 1
I got the answer in rstudio community.我在 rstudio 社区得到了答案。 The solution is the sequence has to be explicitly defined.解决方案是必须明确定义序列。
data_arg <- dt1 %>%
mutate(flagn = case_when(
flag1 == "low" ~ 0,
flag1 == "med" ~ 0.5,
flag1 == "high" ~ 1)) %>%
pull(flagn)
Once this is defined we just pass it to dyRibbon
.一旦定义了它,我们只需将它传递给dyRibbon
。
...
dyRibbon(data = data_arg, palette = brewer.pal(3,"Set1"))
So I was relying on the default sequence of the factor which could be arbitrary.所以我依赖于可能是任意的因子的默认序列。 It is better to define the data argument explicitly.最好明确定义 data 参数。
Thanks to https://community.rstudio.com/t/how-does-the-sequence-of-palette-color-in-dyribbon-get-decided/137559/2?u=sanjmeh感谢https://community.rstudio.com/t/how-does-the-sequence-of-palette-color-in-dyribbon-get-decided/137559/2?u=sanjmeh
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.