首页 > 解决方案 > 使用 for 循环将 ggplot2 对象存储在列表中

问题描述

我正在尝试使用 tidyverse 集合中的包来可视化此处列出的数据集: https ://www.kaggle.com/ruslankl/mice-protein-expression 该数据集包含 80 多个变量。我想总共制作 77 个图,每个图都有解释变量 Genotype,针对数据集中的 77 个其他感兴趣的变量中的每一个进行绘制。我尝试使用包含 ggplot 函数的 for 循环来执行此操作,该函数将遍历所有 77 列并将每个结果图存储在列表中:

DCN_byclass = group_by(Data_Cortex_Nuclear, class, Genotype) %>%
  select(-Behavior, -Treatment, -MouseID) %>%
  summarise_each(funs(mean(., na.rm = TRUE)))
classes = c('c-CS-s','c-CS-m','c-SC-s','c-SC-m','t-CS-s','t-CS-m','t-SC-s','t-SC-m')
desc = c('control mice, stimulated to learn, injected with saline',
         'control mice, stimulated to learn, injected with memantine',
         'control mice, not stimulated to learn, injected with saline',
         'control mice, not stimulated to learn, injected with memantine',
         'trisomy mice, stimulated to learn, injected with saline',
         'trisomy mice, stimulated to learn, injected with memantine',
         'trisomy mice, not stimulated to learn, injected with saline',
         'trisomy mice, not stimulated to learn, injected with memantine')
class_desc = tibble(class = classes, description = desc)
DCN_byclass = left_join(DCN_byclass, class_desc, by = 'class')
DCN_byclass = select(DCN_byclass, class, description, Genotype, everything())
plots = list()
for (i in 1:77) {
plots[i] =
ggplot(DCN_byclass, mapping = aes(Genotype, DCN_byclass[,(i+3)], fill = class)) +
  geom_col(position = 'dodge')
}

但是,循环会返回 50 个此警告的实例:

number of items to replace is not a multiple of replacement length

当我尝试通过索引从列表中访问绘图时,会返回一个小标题而不是绘图:

> plots[1]
[[1]]
# A tibble: 8 x 81
# Groups:   class [8]
  class description Genotype DYRK1A_N ITSN1_N BDNF_N
  <chr> <chr>       <chr>       <dbl>   <dbl>  <dbl>
1 c-CS~ control mi~ Control     0.480   0.653  0.339
2 c-CS~ control mi~ Control     0.597   0.772  0.342
3 c-SC~ control mi~ Control     0.273   0.436  0.291
4 c-SC~ control mi~ Control     0.275   0.449  0.313
5 t-CS~ trisomy mi~ Ts65Dn      0.619   0.797  0.313
6 t-CS~ trisomy mi~ Ts65Dn      0.526   0.760  0.305
7 t-SC~ trisomy mi~ Ts65Dn      0.330   0.567  0.321
8 t-SC~ trisomy mi~ Ts65Dn      0.337   0.549  0.326
# ... with 75 more variables: NR1_N <dbl>, NR2A_N <dbl>,

有没有办法重写这个循环,让它返回所需的图?

标签: rggplot2

解决方案


您应该使用plots[[i]]而不是plots[i].
这样做的原因是[[返回列表中元素的值,而[返回一个新列表,请参阅subsetting
plots[[i]]允许将绘图对象存储在列表中的索引 i 处plots:这是您打算做的。
Usingplots[i]将绘图对象的第一个元素(即它的数据)存储在plots列表的第一个元素中,并发出警告,因为 ggplot 对象是由 9 个元素(data, layers, scales,...)组成的列表,并且只有第一个元素可以分配。

library(ggplot2)
plots <- list()
for (i in 1:2) {
  plots[i] =
    ggplot(mtcars)+geom_point(aes(x=cyl,y=hp))
}
#> Warning in plots[i] <- ggplot(mtcars) + geom_point(aes(x = cyl, y = hp)): 
#> number of items to replace is not a multiple of replacement length

#> Warning in plots[i] <- ggplot(mtcars) + geom_point(aes(x = cyl, y = hp)): 
#> number of items to replace is not a multiple of replacement length
plots[1]
#> [[1]]
#>                      mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
#> ...

for (i in 1:2) {
  plots[[i]] =
    ggplot(mtcars)+geom_point(aes(x=cyl,y=hp))
}
plots[[1]]

reprex 包(v0.3.0)于 2020-07-19 创建


推荐阅读