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

6

我希望使用相同的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文件。


3
这个 like this 类似这样吗? - aosmith
3个回答

1
这是另一种方法:

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() 函数处理数据框的每一行并生成单独的图表。

1

首先,我将您的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))

我将您的“种植”和“玉米”信息拼在一起,创建了一个单独的列:
df[ , plantingCrop := paste(df$planting, df$crop, sep = "-") ]

我创建了一个字符向量,其中包含所有种植作物的组合。稍后你将会明白原因:
plantingCrop1 <- unique(df$plantingCrop)

我使用gridExtra创建所有图形,每个图形都在单独的.pdf页面上。 我基本上创建了一个循环,绘制与我上面创建的plantingCrop1对象中的字符数相同的许多图形。 在每个循环中,dat是您要绘制的子集组,由从我们将种植作物列粘贴在一起时的唯一种植作物组确定。 它重复此操作直到完成为止。
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),因为它可以更好地显示在图表上,但目前我保留它以便你看到不同的组别。

0

我遇到了类似的问题。

我简单地使用了

gridExtra()

这里有可用资源

然后,我使用了另一个包:

cowplot()

这里有可用资源

然后我按照这里的步骤进行操作。

结果非常完美。 :D

希望能对你有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接