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

如何使用R中的数据帧列表绘制图表,r,ggplot2,graphics,plotly,r-plotly,R,Ggplot2,Graphics,Plotly,R Plotly,我在一个列表中有几个数据帧(40),我需要一种方法来创建一个图表,可以使用plotly或ggplot2 我在下面放了一个示例模板,说明我目前如何创建图表 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", "M

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

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

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

aa从数据列表中绘制所有数据。框架可以使用基本函数完成

par(mfrow = c(1, length(abc)))    # optional, divides plotting area
lapply(abc, FUN = function(x) barplot(x[, 2], names.arg = x[, 1]))
但是,覆盖现有绘图需要针对所需data.frame执行选择步骤

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

我调整了您的代码,使其在循环中运行

library(plotly)

p使用
lappy
rect
不是更容易吗?@nya我在r:dthaaaanks中是n00b!很高兴这有帮助!
for(i in 1:length(abc)){
   ...
   waiting <- readline("Press enter to view next plot")
}
# 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"))