问题
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)
Basic ggplot:
library(ggplot2)
ggplot()+
geom_point(aes(x=xvalue, y=yvalue), data=df)
but instead of facet_grid
to get the location x planting x crop combos, I want one separate pdf of each.
回答1:
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))
I pasted together your planting
and corn
information to create a separate column:
df[ , plantingCrop := paste(df$planting, df$crop, sep = "-") ]
I created an character vector that has all combinations of planting
and crop
. You'll see why in a second:
plantingCrop1 <- unique(df$plantingCrop)
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()
I also included the right way to name the files using the plantingCrop
name as a subtitle. That's in the ggtitle
call.
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.
回答2:
Here's another approach:
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)
The file names look like early_corn_A.pdf and are saved in your current working directory. I set fixed x/y limits for visual convenience. The function takes
a one-row data frame as input and outputs a plot in pdf form. The plyr::d_ply()
function processes each row of the data frame and produces a separate plot per row.
回答3:
I came across a similar problem.
I simply used
gridExtra()
Available here
Then, I used another package named :
cowplot()
Available here
Then I followed the steps here.
And it worked like a charm. :D
Hope it helps.
来源:https://stackoverflow.com/questions/30312135/produce-multiple-ggplot-figures-within-one-ggplot