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

时间:2018-05-28 07:15:49

标签: r ggplot2 graphics plotly r-plotly

我在列表中有几个数据框(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。

2 个答案:

答案 0 :(得分:2)

我调整了你的代码以循环运行

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"))

答案 1 :(得分:1)

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

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: ")

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

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

在两种选择中,i将包含具有相应data.frame的列表名称或编号。然后我们可以使用它来选择绘图数据。请注意,列表中的元素使用双方括号和带有单个方括号的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))