尝试对不同的组使用不同的颜色会破坏R

时间:2019-08-19 06:53:12

标签: r colors slider plotly

我对绘图很陌生,并尝试在R中绘制一个条形图,其中将一个滑块和一个矢量作为组(以疾病为例),并用不同的颜色标记。在单个图形中,这种方法可以正常工作,但是添加了滑块后,图形中显示的数据将与显示的数据不匹配。

在新绘制的图形中出现的初始图形是正确的,但是当我开始移动滑块时,绘制的数据是从不同的图形中获取的,而不是从应有的图形中获取的所有数据。我已经知道这是颜色参数的问题。如果我将其取出,它会正确绘制图形(尽管仍然是错误的步骤)。我尝试使用组参数代替,据我所知,它应该做同样的事情,但是导致错误,因为“组参数已被弃用”。它建议使用group_by()或使用split代替,但我都没有设法使它们全部起作用。

这是我能够弄清楚它使用什么数据的东西:

  1. 整个彩色组的数据都来自同一位置(步骤)。因此,在我的数据中,“胃肠炎”组的数据来自同一图的数据,“死亡”的数据也来自该数据。

  2. 如果我按字母顺序查看组,则每三分之二的数据(在这种情况下为三步)是从同一位置(一步)获取的。例如,当在滑块上选择步骤0时,实际绘制的是步骤1的cbgs,步骤2的死亡,步骤0的esrd,步骤1的肠胃炎,步骤2的hus,等等。

我一直无法弄清楚为什么。这是我的代码:

library(plotly)

y <- c("campylobacter", "campylobacter", "campylobacter", "campylobacter", "campylobacter",
       "cryptosporidium", "E.coli O157:H7", "norovirus", "rotavirus", "E.coli O157:H7",
       "cryptosporidium", "E.coli O157:H7", "norovirus", "rotavirus", "E.coli O157:H7",
       "E.coli O157:H7", "giardia")

resp <- c("cgbs", "death", "gastroenteritis", "ra", "rgbs", "death", "death", "death", "death",
       "esrd", "gastroenteritis", "gastroenteritis", "gastroenteritis", "gastroenteritis",
       "hus", "hc", "gastroenteritis")


aval <- list(list(visible = TRUE,
                  name = 'dose = 0',
                  x=c(0.045, 4.105, 2.245, 0.377, 0.902, 24.183, 82.154, 3.012, 0.735, 84.181,
                      7.772, 2.695, 4.644, 8.240, 4.388, 23.085, 34.710),
                  y=y,
                  resp=resp),
             list(visible = FALSE, 
                  name = 'dose = 0.1',
                  x=c(0.0193, 1.754, 0.959, 0.161, 0.385, 24.183, 1.900, 0.011, 0.003, 1.947,
                      7.772, 0.062, 0.017, 0.031, 0.101, 0.534, 34.708),
                  y=y,
                  resp=resp),
             list(visible = FALSE, 
                  name = 'dose = 0.2',
                  x=c(0.0188, 1.710, 0.935, 0.157, 0.376, 24.183, 0.185, 0.010, 0.003, 0.189,
                      7.772, 0.006, 0.016, 0.031, 0.010, 0.052, 34.706),
                  y=y,
                  resp=resp))


## create steps and plot all traces
steps <- list()
p <- plot_ly(type = 'bar', orientation = "h", y=aval[1][[1]]$y, x=c(rep(0, 17)), showlegend=F)
for (i in 1:length(aval)) {
  p <- add_trace(p, # add bars to bar graph p
                 x=aval[i][[1]]$x, # x (BoD) on x
                 y=aval[i][[1]]$y, # y (exposure agent) on y
                 visible = aval[i][[1]]$visible, # visibility was set earlier
                 text = aval[i][[1]]$resp,
                 type="bar",
                 hoverinfo = "text+x", # what the info shown when mouse on top of it says
                 color= aval[i][[1]]$resp, # different diseases with different colors
                 orientation="h",
                 showlegend=T)

  step <- list(args = list('visible', rep(FALSE, length(aval))), # set all steps to not visible
               method = 'restyle',
               label= (i-1)/10) # Sets the text label to appear on the slider 
  step$args[[2]][i] = TRUE  # set data on i step to be visible when step i is chosen
  steps[[i]] = step # make a list with all the steps
}  



## add slider control to plot
p <- p %>%
  layout(sliders = list(list(active = 0, 
                             currentvalue = list(prefix = "Chlorine dose (mg/l): "), 
                             steps = steps))) %>%
  layout(barmode = "stack", # stacked plot instead of each disease its own bar
         title = paste("Burdens of disease of drinking water"),
         xaxis = list(title ="DALY"),
         yaxis = list(title =""))



p

非常感谢您的帮助。

0 个答案:

没有答案