I have searched around for a solution to this, but it appears that most deal with individually generated plots being combined into PDF format, rather than separating out plots generated using faceting onto separate pages of a PDF.
Example Data
In the above data using the following code, selecting all items in the generated list:
Ex<-read.csv("StackOverflowEx (3).csv")
library(ggplot2)
library(reshape2)
vars <- select.list(names(Ex),multiple=TRUE,graphics=TRUE)
Cases<-subset(Ex,select=vars)
gg<-melt(Cases,id=c("Item","Total","Admin"))
print(ggplot(gg, aes(x=Total,y=Admin))+
geom_point(colour="dark green",size=1.5)+
geom_point(aes(y=value,color=variable))+
geom_smooth(aes(y=value,fill=variable),
method=loess,size=1,linetype=1,se=T)+
facet_wrap(~variable,ncol=2,nrow=1000)+
ylim(0,1)+
labs(x="Expected",y="Admin",title=vars))
...should generate a facet wrap of all 8 (A-H) Cases. However, generating this tends to cramp the plots and make them less readable, and in practice, I intend to use this on 500+ cases (which just returns bars labeled with the column name with no readable charts).
Is it possible to specify the number of charts in the faceting to appear on an individual page when converting to PDF, rather than having all plots compress into a single page? For example, using the above data, generating two 2x2 plots on separate pages that contain all 8 cases individually (i.e. Cases A-D on pg 1, Cases E-H on pg 2).
I could do this by highlighting 4 cases + "Item", "Total" & "Admin" and repeating for the next 4 cases and combining the resultant PDFs. However with upwards of 500 cases in practice this would mean more than 100 iterations with lots of potential for human error. Some help with automating the process would be great.
As there is no answer yet. Here is one:
library(ggplot2)
library(reshape2)
Ex <- read.csv("C:/Users/Thomas/Desktop/StackOverflowEx (3).csv")
gg <- melt(Ex, id = c("Item", "Total", "Admin"))
# put in number of plots here
noPlots <- 4
# save all variables in a seperate vector to select in for-loop
allVars <- unique(gg$variable)
noVars <- length(allVars)
# indices for plotting variables
plotSequence <- c(seq(0, noVars-1, by = noPlots), noVars)
# pdf("plotpath.pdf") # uncomment to save the resulting plots in a pdf file
# loop over the variables to plot
for(ii in 2:length(plotSequence)){
# select start and end of variables to plot
start <- plotSequence[ii-1] + 1
end <- plotSequence[ii]
# subset the variables and save new temporary data.frame
tmp <- subset(gg, variable %in% allVars[start:end])
cat(unique(tmp$variable), "\n")
# generate plot
p <- ggplot(tmp, aes(x = Total, y = Admin))+
geom_point(colour = "dark green", size = 1.5)+
geom_point(aes(y = value, color = variable))+
geom_smooth(aes(y = value, fill = variable),
method = loess, size = 1, linetype = 1, se = T)+
facet_wrap(~variable, ncol = 2, nrow = 1000)+
ylim(0, 1)+
labs(x = "Expected", y = "Admin")
print(p)
}
# dev.off()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With