【问题标题】:ggplot on grid with a grobList in R网格上的 ggplot 与 R 中的 grobList
【发布时间】:2018-07-20 20:18:30
【问题描述】:

我正在尝试在 for 循环中使用 ggplot2 在网格上绘制多个图,然后是 grid.arrange。但之后所有的情节都是一样的。

library(ggplot2)
library(grid)
test = data.frame(matrix(rnorm(320), ncol=16 ))
names(test) = sapply(1:16, function(x) paste0("var_",as.character(x)))
plotlist = list()
for (i in 1:(dim(test)[2]-1)){
    plotlist[[i]] = ggplot(test) + 
        geom_point(aes(get(x=names(test)[dim(test)[2]]), y=get(names(test)[i])))
}
pdf("output.pdf")
do.call(grid.arrange, list(grobs=plotlist, nrow=3))
dev.off(4)

运行此代码时,似乎 get() 调用仅在 grid.arrange 调用时进行评估,因此图中的所有 y 向量都与“var_15”相同。有没有办法强制立即进行评估,以便我得到 15 个不同的地块?

谢谢!

【问题讨论】:

  • 查看相关问题和答案,例如herehere。我认为使用aes_string() 是最简单的,但最新版本的ggplot2 正在向tidyeval 发展。

标签: r ggplot2 visualization grid-layout


【解决方案1】:

这里有两种使用purrr::map 函数而不是for 循环的方法。我发现当我尝试使用循环时,我对正在发生的事情不太清楚,而且由于有像 applymap 这样的函数非常适合 R 的向量操作范式,所以我通常会选择而是映射。

第一个例子使用cowplot::plot_grid,它可以获取一个地块列表并排列它们。第二个使用较新的patchwork 包,它允许您将绘图添加在一起——就像字面上说的plot1 + plot2——并添加布局。为了完成所有这些添加,我使用 purrr::reduce+ 作为应用于所有绘图的函数。

library(tidyverse)

set.seed(722)
test = data.frame(matrix(rnorm(320), ncol=16 ))
names(test) = sapply(1:16, function(x) paste0("var_",as.character(x)))


# extract all but last column
xvars <- test[, -ncol(test)]

通过使用purrr::imap,我可以映射所有列并应用一个具有两个参数的函数:列本身及其名称。这样我就可以设置一个指定列名的 x 轴标签。我还可以轻松访问数据列,而无需使用 get 或任何 tidyeval 技巧(尽管对于复杂的东西,tidyeval 解决方案可能会更好)。

plots <- imap(xvars, function(variable, var_name) {
  df <- data_frame(x = variable, y = test[, ncol(test)])
  ggplot(df, aes(x = x, y = y)) +
    geom_point() +
    xlab(var_name)
})

cowplot::plot_grid(plotlist = plots, nrow = 3)

library(patchwork)

# same as plots[[1]] + plots[[2]] + plots[[3]] + ...
reduce(plots, `+`) + plot_layout(nrow = 3)

reprex package (v0.2.0) 于 2018 年 7 月 22 日创建。

【讨论】:

    【解决方案2】:

    试试这个:

    library(ggplot2)
    library(grid)
    library(gridExtra)
    set.seed(1234)
    test = data.frame(matrix(rnorm(320), ncol=16 ))
    names(test) = sapply(1:16, function(x) paste0("var_",as.character(x)))
    plotlist = list()
    for (i in 1:(dim(test)[2]-1)) {
      # Define here the dataset for the i-th plot
      df <- data.frame(x=test$var_16, y=test[, i])
      plotlist[[i]] = ggplot(data=df, aes(x=x, y=y)) + geom_point()
    }
    grid.arrange(grobs=plotlist, nrow=3)
    

    【讨论】:

      猜你喜欢
      • 2021-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-23
      • 2018-04-16
      • 2021-07-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多