【问题标题】:ggplot: plot geom_boxplot from list of elements of different lengthggplot:从不同长度的元素列表中绘制 geom_boxplot
【发布时间】:2015-06-01 23:15:18
【问题描述】:

我有这个时间序列的元素分组在一个长度为t的列表中。

series <- seq(1:10)

lst <- list()

set.seed(28100)
for (t in series) {
  lst[[t]] <- sample(c(1:20, NA), sample(1:20, 1))
}

列表元素的长度可以变化;在列表中创建一个二维 data.frame 显然是不可行的:

lst
# [[1]]
# [1]  6  7 12  4 15 20  3
# 
# [[2]]
# [1] 14 18  8 20 NA  6 19  4  9  5  1 13  3 10 12 15
# [17] 11 17
# 
# ...
# 
# [[9]]
# [1]  3  9 12  8 16 15 10 19 14 11  6  2 20 13  5 18
# 
# [[10]]
# [1]  4 20 10  2 12  5 19  1 NA 11 14  7 17

我仍然想用geom_boxplot() 创建一个时间序列箱线图(例如this),包括我分布的异常值。

【问题讨论】:

    标签: r ggplot2 boxplot


    【解决方案1】:

    如果您尝试在 x 轴上绘制 series 并在 y 轴上绘制采样值(我将它们称为 y),则创建一个数据帧列表并将它们堆叠以获得ggplot需要的数据结构。例如:

    library(ggplot2)
    
    # Modify lst into data frames of varying dimension
    lst <- lapply(series, function(x) {
      data.frame(series = factor(x, levels = series),
                 y = lst[[x]])
    })
    
    # Stack the data frames
    lst <- do.call(rbind, lst)
    
    # Make the plot
    ggplot(lst, aes(x = series, y = y)) +
      geom_boxplot()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-13
      • 2013-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多