Thanks a lot for your reply Trevor! I've been working with the code but I cannot make it work. I have 2 main problems: 1. From running the loop I get pdf files with no pages generated. 2. I don't know how to write the code to get the 8 sex/day combinations.
library(ggplot2) library(reshape2) sex1 <- as.character(unique(tips$sex)) day1 <- as.character(unique(tips$day)) for (i in 1:length(sex1)){ for (j in 1:length(day1)){ pdf(sprintf("C:/Users/bgonzale/Desktop/example_%s.pdf", i, j)) data1 <- subset(tips, sex==sex1[i] & day==day1[j]) ggplot(data1, aes(x=total_bill, y=tip/total_bill)) + geom_point(shape=1) dev.off() }} Thanks very much! On 30/06/2014 19:35, Trevor Davies wrote: > I think the easiest most straight forward way would be to just throw > it into a loop and subset the data on each loop around (untested code > below but I'm sure you get the gist). ~Trevor > > sex1<-unique(tips$sex) > day1<-unique(tips$day) > for (i in 1:length(sex1)){ > for (j in 1:length(day1)){ > > > pdf(paste('example_',sex1[i],day1[j],'.pdf',sep='')) > > data1<-subset(tips, sex==sex1[i] & day==day1[j]) > sp <- ggplot(data1,aes(x=total_bill, y = tip/total_bill)) > +geom_point(shape=1) > plot(sp) > dev.off() > } > } > > > > On Mon, Jun 30, 2014 at 10:23 AM, Bea GD <aguitatie...@hotmail.com > <mailto:aguitatie...@hotmail.com>> wrote: > > Hi, > > I'm working with tips data from reshape package. > > library(reshape2) > > I'm saving my plots as pdf and I was wondering whether it was possible > to print a different pdf for each 'wrapped' plot. Using the code below > as an example, I'd like to get 8 independent pdf files for each sex ~ > day combination. > > sp <- ggplot(tips,aes(x=total_bill, y = tip/total_bill)) + > geom_point(shape=1) + > facet_grid(sex ~ day) > plot(sp) > > Thanks a lot for your help! > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org <mailto:R-help@r-project.org> mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.