如何使用 R 中的数据框列表绘制图表

How to plot a chart using a list of dataframes in R

我在一个列表中有几个数据框 (40),我需要一种方法来创建一个图表,该图表可以使用 plotlyggplot2

根据所选数据框进行修改

我在下面放了一个示例模板,说明我目前是如何创建图表的。

aa <- c("José", "Sue")
ab <- c(10, 40)
a <- data.frame(aa,ab)

ba <- c("Marie", "Pablo")
bb <- c(25, 20)
b <- data.frame(ba,bb)

ca <- c("Elizabeth", "Mike")
cb <- c(50, 20)
c <- data.frame(ca,cb)

abc <- list(a,b,c)

names(abc) <- c("a","b","c")



p <- plot_ly() %>%
  add_trace(type = 'bar', x = abc$a$aa,
            y = abc$a$ab, visible=T, marker = list(color = 'blue'))  %>%

  add_trace(type = 'bar',x = abc$b$ba,
            y = abc$b$bb, visible=F, marker = list(color = 'red')) %>%

  add_trace(type = 'bar', x = abc$b$ba,
            y = abc$c$cb, visible=F, marker = list(color = 'yellow'))  %>%

  layout(
    updatemenus = list(
      list(
        yanchor = 'auto',
        buttons = list(
          list(method = "restyle",
               args = list("visible", list(T, F,F)),
               label = 'a'),

          list(method = "restyle",
               args = list("visible", list(F,T, F)),
               label = 'b'),

          list(method = "restyle",
               args = list("visible", list(F, F,T)),
               label = 'c')
        ))))
p

示例:(p) Link of the graphic

我需要一种方法来覆盖多个 add_trace 的创建,使用 x = abc$a$aa 和 y = abc$a$ab,对于列表中访问的每个数据帧。

绘制 data.frames 列表中的所有数据可以使用基本函数完成。

par(mfrow = c(1, length(abc)))    # optional, divides plotting area
lapply(abc, FUN = function(x) barplot(x[, 2], names.arg = x[, 1]))

但是,覆盖现有绘图需要 selection 步骤以获得所需的 data.frame

i <- readline("Enter the name of the data: ")

或等待用户查看数据的循环

for(i in 1:length(abc)){
   ...
   waiting <- readline("Press enter to view next plot")
}

在这两种选择中,i 将包含列表名称或编号以及相应的 data.frame。然后我们可以使用它来 select 数据进行绘图。请注意,列表中的元素是用双方括号检索的,而 data.frame 中的元素是用单方括号检索的。

# finds plotting area limits
lims <- max(unlist(lapply(abc, function(x) x[,2])))
# plots chosen data
barplot(abc[[i]][,2], names.arg = abc[[i]][,1], ylim = c(0, lims))

我在一个循环中将你的代码调整为 运行

library(plotly)

p <- plot_ly() 
layout_buttons <- list()
color_list <- palette()  #color list - you may define your own list of colors here
visible_default_layout <- c(T, rep(F, length(abc)-1))    

for (i in seq_along(abc)){
  p <- p %>% add_trace(x = abc[[i]][[1]], y = abc[[i]][[2]], 
                       type = 'bar', visible=visible_default_layout[i], marker = list(color = color_list[i]))
  
  visible_layout <- rep(F, length(abc))
  visible_layout[i] <- T
  layout_buttons[[i]] <- list(method = "restyle",
                              args = list("visible", as.list(visible_layout)),
                              label = names(abc)[i])
  }

p <- p %>%
  layout(updatemenus = list(list(yanchor = 'auto', buttons = layout_buttons)))
p

示例数据:

abc <- structure(list(a = structure(list(aa = structure(1:2, .Label = c("José", 
"Sue"), class = "factor"), ab = c(10, 40)), .Names = c("aa", 
"ab"), row.names = c(NA, -2L), class = "data.frame"), b = structure(list(
    ba = structure(1:2, .Label = c("Marie", "Pablo"), class = "factor"), 
    bb = c(25, 20)), .Names = c("ba", "bb"), row.names = c(NA, 
-2L), class = "data.frame"), c = structure(list(ca = structure(1:2, .Label = c("Elizabeth", 
"Mike"), class = "factor"), cb = c(50, 20)), .Names = c("ca", 
"cb"), row.names = c(NA, -2L), class = "data.frame")), .Names = c("a", 
"b", "c"))