在一个ggplot()中产生多个ggplot图形 [英] Produce multiple ggplot figures within one ggplot()
问题描述
我想使用相同的ggplot代码生成8个不同的图,这些图以我的数据帧中的图为条件.通常,我会使用facet_grid,但是在这种情况下,我希望最终得到每个人物的pdf文件.例如,我想在这里为每一行提供一个pdf文件:
I would like to use the same ggplot code to produce 8 different figures conditional upon figures in my dataframe. Usually I would use facet_grid, but in this case, I would like to end up with a pdf of each individual figure. For example, I would like one pdf for each row here:
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)
但是要获取x种植x作物组合的位置,而不是 facet_grid
,我希望每个都有一个单独的pdf.
but instead of facet_grid
to get the location x planting x crop combos, I want one separate pdf of each.
推荐答案
首先,我将您的MWE设置为 data.table
,因为它速度更快
First I made your MWE into a data.table
because it's faster
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))
我将您的 planting
和 corn
信息粘贴在一起以创建单独的列:
I pasted together your planting
and corn
information to create a separate column:
df[ , plantingCrop := paste(df$planting, df$crop, sep = "-") ]
我创建了一个字符向量,其中包含 planting
和 crop
的所有组合.一秒钟您会明白为什么:
I created an character vector that has all combinations of planting
and crop
. You'll see why in a second:
plantingCrop1 <- unique(df$plantingCrop)
我使用 gridExtra
在单独的 .pdf
页中创建所有图.我基本上创建了一个循环,该循环绘制的图形与上面我在 plantingCrop1
对象中存在的字符一样多.在每个循环中, dat
是您要绘制的子集组,通过粘贴 planting
和 plantingCrop
组> crop 列在一起.重复此过程,直到一切完成.
I use gridExtra
to create all of the plots in separate .pdf
pages. I basically created a loop that plots as many graphs as there are characters in the plantingCrop1
object I made above. In each loop, dat
is the subsetted group that you want to plot, by the unique plantingCrop
group from when we pasted the planting
and crop
columns together. It repeats this until everything is done.
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
调用中.
I also included the right way to name the files using the plantingCrop
name as a subtitle. That's in the ggtitle
call.
我建议您在显示更多数据时,将 geom_boxplot(aes(color = location))
更改为 geom_boxplot(aes(fill = location)
)更好地显示在图表上,但是我现在就这样保留了,以便您可以看到不同的组.
I'd encourage you to change geom_boxplot(aes(color = location))
to geom_boxplot(aes(fill = location)
when you have more data as it shows up on a graph better, but I left it that way for now so you can see the different groups.
这篇关于在一个ggplot()中产生多个ggplot图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!