在一个 ggplot() 中生成多个 ggplot 图

Produce multiple ggplot figures within one ggplot()

我想使用相同的 ggplot 代码根据数据框中的数字生成 8 个不同的数字。通常我会使用 facet_grid,但在这种情况下,我想以每个单独图形的 pdf 结尾。例如,我希望这里的每一行都有一个 pdf:

df <- read.table(text = "
xvalue     yvalue    location    planting    crop
  1          5          A          early      corn
  2          3          A          late       corn
  6          2          A          early      soy
  7          4          A          late       soy
  4          7          S          early      corn
  2          6          S          late       corn
  3          2          S          early      soy
  5          1          S          late       soy
", sep = "", header = TRUE)

基本 ggplot:

library(ggplot2)

ggplot()+
  geom_point(aes(x=xvalue, y=yvalue), data=df)

但不是 facet_grid 获取位置 x 种植 x 作物组合,我想要每个单独的 pdf。

这是另一种方法:

library(plyr)
library(ggplot2)

df <- read.table(text = "
xvalue     yvalue    location    planting    crop
  1          5          A          early      corn
  2          3          A          late       corn
  6          2          A          early      soy
  7          4          A          late       soy
  4          7          S          early      corn
  2          6          S          late       corn
  3          2          S          early      soy
  5          1          S          late       soy
", sep = "", header = TRUE)

fplot <- function(d)
  {
     require(ggplot2)
     p <- ggplot(d, aes(x = xvalue, y = yvalue)) +
             geom_point(size = 4) +
             xlim(0, 10) + ylim(0, 10)
     file <- with(d, paste0(paste(planting, crop, location, sep = "_"), ".pdf"))
     ggsave(file, p)
  }

d_ply(df, ~ rownames(df), fplot)

文件名类似于 early_corn_A.pdf 并保存在您当前的工作目录中。为了视觉方便,我设置了固定的 x/y 限制。该功能需要 一个单行数据框作为输入并以 pdf 格式输出一个图。 plyr::d_ply() 函数处理数据框的每一行并为每行生成一个单独的图。

我遇到了类似的问题。

我只是用了

gridExtra()

可用here

然后,我使用了另一个名为 :

的包

cowplot()

可用here

然后我按照步骤here

而且效果非常好。 :D

希望对您有所帮助。

首先我把你的 MWE 变成了 data.table 因为它更快

library(data.table)
library(ggplot2)
library(gridExtra)

df <- data.table(read.table(text = "
            xvalue     yvalue    location    planting    crop
            1          5          A          early      corn
            2          3          A          late       corn
            6          2          A          early      soy
            7          4          A          late       soy
            4          7          S          early      corn
            2          6          S          late       corn
            3          2          S          early      soy
            5          1          S          late       soy
            ", sep = "", header = TRUE))

我将您的 plantingcorn 信息粘贴在一起以创建一个单独的列:

df[ , plantingCrop := paste(df$planting, df$crop, sep = "-") ]

我创建了一个包含 plantingcrop 的所有组合的字符向量。你马上就会明白为什么:

plantingCrop1 <- unique(df$plantingCrop)

我使用 gridExtra 在单独的 .pdf 页面中创建所有情节。我基本上创建了一个循环,它绘制的图形与上面制作的 plantingCrop1 object 中的字符一样多。在每个循环中,dat 是您要绘制的子集组,根据我们将 plantingcrop 列粘贴在一起时的唯一 plantingCrop 组。它重复这个直到一切都完成。

pdf("plantingCrop.pdf", onefile = TRUE)
for(i in 1:length(plantingCrop1)){
        dat <- subset(df, plantingCrop==plantingCrop1[i])
        cropPlot <- ggplot(dat, aes(xvalue,yvalue)) + 
                    geom_boxplot(aes(color = location)) + 
                    theme_bw() + 
                    ggtitle(bquote(atop(.("Boxplot of Stuff"), 
                          atop(italic(.(plantingCrop1[i])), "")))) +
                    labs(x = "xvalue", y = "yvalue") +
                    theme(legend.position = "top", legend.title=element_blank())
        grid.arrange(cropPlot)
        }
dev.off()

我还包括使用 plantingCrop 名称作为副标题命名文件的正确方法。那是在 ggtitle 调用中。

当你有更多数据时,我鼓励你将 geom_boxplot(aes(color = location)) 更改为 geom_boxplot(aes(fill = location),因为它在图表上显示得更好,但我暂时保留它以便你可以看到不同的群体。