0

I have a list object with several (64) data frames. While I could successfully manage to plot multiple histograms for each data frame in a loop (in a 64-page PDF document, with each plot on a new page), I am having a hard time producing a 8 page PDF document with 8 plots per page.
The code I have used :

    listfinal1 <- list()
    listfinal2 <- list()
    for (i in 1:64){
    listfinal1[[i]] <- data.frame(rnorm(100,10))
    colnames(listfinal1[[i]]) <- c("Column A")
    }
    for (i in 1:64){
    listfinal2[[i]] <- data.frame(rnorm(10,1))
    colnames(listfinal2[[i]]) <- c("Column B")
    }
    plot_list <- list()
    pdf("jnk1.pdf")
    plotlist <- list()
    for (i in 1: length(listfinal1)){
    P1 <- hist(listfinal1[[i]][,1],breaks=seq(-30,30,by=2),plot=FALSE)
    P2 <- hist(listfinal2[[i]][,1],breaks=seq(-30,30,by=2),plot=FALSE)
    P3 <- plot(0,0,type="n",xlim=c(-30,30),ylim= c(0,max(P1$counts)+2),xlab="Costum Lenghts (mm)",ylab= "Frequency")
    grid(col="lightgray",lty="solid")
    plot1 <- plot(P1,col="black",density=0,angle=135,add=TRUE)
    plot2 <- plot(P2,col="red",density=50,angle=45,add=TRUE)
    plotlist[[i]] <- plot2
     }

     dev.off()
Mansi
  • 133
  • 2
  • 11
  • If I understand correctly, you want to call grid.arrange in each for loop? – Hardik Gupta Jan 07 '17 at 07:28
  • check the answer here - http://stackoverflow.com/questions/9315611/grid-of-multiple-ggplot2-plots-which-have-been-made-in-a-for-loop – Hardik Gupta Jan 07 '17 at 07:30
  • Please provide a reproducible example http://stackoverflow.com/help/mcve. For `grid.arrange`, llok in the help the different option (`layout_matrix`,`widths`, `heights`) – timat Jan 08 '17 at 10:06
  • @Mansi you actually change your question, and your not using `ggplot2`. So `grid.arrange` won't work. Also the tag `ggplot2` should be removed or update the question with `ggplot2` plot – timat Jan 09 '17 at 10:03

1 Answers1

0

1- base graphics draw immediately, you can't assign them to a variable

2- par(mfrow=c(4,2)) will place 4x2 plots per page

pdf('test.pdf')
par(mfrow=c(2,1))
for(ii in 1:3){
  plot(1,1)
  hist(rnorm(10))
}
dev.off()
baptiste
  • 75,767
  • 19
  • 198
  • 294