1

Having an unusual problem with creating multiple files in R with ggplot2.

I've got multiple plots to create for multiple people, so I'm creating all the plots for each person in a pdf. So it goes something like this...

for(i in 1:10)
{
    pdf(paste("person",i,".pdf",sep=""))
    ggplot2(...)+.........
    ggplot2(...)+.........
    ggplot2(...)+.........
    ggplot2(...)+.........
    dev.off()
}

I've verified that all the code to create the plots is working and that creating a single pdf works, no problems there. The problem arises when I try to run the loop, it creates the files, but they're blank. I've tried everything I can think of and can't seem to find any information about this. I've tried in RStudio (Windows) and command line (ubuntu), both create the same issue.

Any insight or an alternative would be appreciated, thanks

1 Answer 1

3

You need to use print for each plot want you output into a pdf.

library(ggplot2)
dat = data.frame(x1=rnorm(10), x2=rnorm(10))
for(i in 1:2){
    pdf(paste("person",i,".pdf",sep=""))
    p1 = ggplot(dat, aes(x=x1)) + geom_histogram()
    p2 = ggplot(dat, aes(x=x2)) + geom_histogram()
    print(p1)
    print(p2)
    dev.off()
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.