pdf('rplotings.pdf')
for (i in 1:length(n_people)){
object3 <- Datarray[, ,i]
object2 <- data.frame(object3)
ggcorrplot(object2) + ggtitle(paste("N=",n_people[i]))
}
dev.off()
What I have tried to do here is place all 20 plots into a single PDF file: the function seems to be running without error but when I try to open the file, it says there is an error?
The reason this is happening is that a loop will only print output if you explicitly tell it to.
# No output printed to console
for(i in 1:10){
i
}
# Output printed
for(i in 1:10){
print(i)
}
So to make sure the PDF captures the ggplot, you need to wrap it in the plot() function
library(ggplot2)
pdf(file="test.pdf")
for(i in 1:10){
plot(ggplot(data.frame(x = 1:10, y = runif(10)), aes(x = x, y = y)) +
geom_line())
}
dev.off()
That should fix it... unless there's something wrong with the plots itself of course, but you can check that by running the code inside the loop manually.