multiple plots saved to single page pdf file (ggplot/grid.arrange?)

Hello,
I would like to get some advice and help on an idea.
At the moment, I can print my different plots in a PDF document with one plot per page for each of my leaders :

for (my_leader in my_leaders_list){     
  pdf(file = paste0(dir,"/figs/PCA_PI_",my_leader,".pdf"))
  barplot(...)
  acp2d(....)
  for (patients in patients_list_of_<my_leader>){
    print(ggplot(......))
  }
dev.off()
}
dev.off()

As you can see on the simplified code, there is a barplot, a ggplot acp representation, one or more ggplots depending on the length of the patient list of each leader.

Leader_1 : patient1, patient2, patient3
Leader_2 : patient1
Leader_3 : patient1, patient2
,etc.

My idea is the following : I want my plots to be on a single pdf page for each of my leaders

I was able to produce at least this :

with :


for (my_leader in my_leaders_list){    
    pdf(file = paste0(dir,"/figs/PCA_PI_",my_leader,".pdf"))
    plot1=acp2d(...)
    ggp = list()
    for (patients in my_patients_leader_list){
                for (i in 1:length(my_patients_leader_list){
                          ggp[[pi]]=ggplot(.....)
    }
  }
  grid.arrange(plot1,ggp[[pi]],nrow=2)
}


I have my acp plot and my plot of the last patient of each leader arranged on my page but several levels of difficulty appear to me for the next step :

  1. loops : I know that r is not made for loops (I still have many bad manners related to python programming) and that there is lapply for example. But I don't know how to apply this tool on my code

  2. I can't create a list of my patient plots to give to grid.arrange if it's possible

  3. I also tried the version with the cowplot and patchwork packages

  4. I sometimes get errors about "grob" : the barplot() format seems to be problematic for grid.arrange for exemple. What is it and when should I convert ggplot to "grob" ?

Thank you for your patience and interest in this beginner's post !

The grob business is probably because your barplot outputs a base plot, while your ggplot outputs a grid plot. These are two different graphics systems, and so not always easy to combine. I don't know what acp2d is.

You can redraw all your plots to use the grid graphics system, and use your grid.arrange approach. But cowplot also has support to combine base and grid plots, see Mixing different plotting frameworks • cowplot.

Loops are ok, but you're right that the apply family is popular. It's hard to translate code for you without a reproducible example though. But this certainly looks odd to me:

for (patients in my_patients_leader_list){
                for (i in 1:length(my_patients_leader_list){
                          ggp[[pi]]=ggplot(.....)
    }

You have nested loops, but both are looping over the same thing (my_patients_leader_list). But then you assign to ggp[[pi]], even though your loops are using patients and i. Surely this wouldn't make sense in python either.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.