【问题标题】:How to plot a chart using a list of dataframes in R如何使用 R 中的数据框列表绘制图表
【发布时间】:2018-11-06 17:31:43
【问题描述】:

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

【问题讨论】:

  • 使用lapplyrect 会更容易吗?
  • @nya i'm n00b in r :D

标签: r ggplot2 graphics plotly r-plotly


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    可以使用基本函数来绘制 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))
    

    【讨论】:

      猜你喜欢
      • 2021-11-24
      • 2018-08-01
      • 2021-12-27
      • 2018-06-20
      • 2020-11-10
      • 2020-01-09
      • 1970-01-01
      • 2021-06-30
      • 2019-07-28
      相关资源
      最近更新 更多