Home > Back-end >  R - Automatize Multiplot Input ggPlot2
R - Automatize Multiplot Input ggPlot2

Time:09-27

I have a list of ggplots called plotlist1. I want to create a multiplot of those plots, who are linked together.

For creating a multiplot I use:

grid.arrange(plotlist[[1]], plotlist1[[1]], plotlist1[[2]])

plotlist[[1]] has to be added as the first plot.

The problem is that I have to do a lot of these multiplot and I want to automatize it. To identify those plots that are related, I created a list of the names of these plots, called namelist.

Plots starting with the same letter are related, so I did this:

position = which(grepl("A", namelist))
n = length(position)

It gives me the position of the plots in the plotlist1, that I want to plot together. My problem is now, how can I create the grid.arrange command automatically? I tried to recreate it with past and replicate, but I am just not getting anywhere.

This is my try:

base1 = paste(replicate(lang, "plotlist1"))
base2 = paste(replicate(n, "[["))
base3 = paste(replicate(n, "]]"))
base4 = paste0(base2, position)
base5 = paste0(base4, base3)
base6 = paste0(base1, base5, collapse = ",")
base7 = paste0("plotlist[[1]],", base6)

grid_arrange(base7)
grid_arrange(as.factor(base7))

The namelist:

c("A00-A09", "A09", "B85-B89", "B99", "C-D4", "F10", "F10-F19", 
"G40", "G40-G47", "G45", "H0-H5", "H00-H06", "H10", "H10-H13", 
"H15-H22", "H16", "H6-H9", "H65", "H65-H75", "I10", "I10-I15", 
"I20", "I20-I25", "I30-I52", "I4-I74", "I50", "I60-I69", "I63", 
"I70-I79", "I80", "I80-I82", "I80-I89", "J00-J06", "J06", "J18", 
"J20", "J20-J22", "J22", "J40", "J40-J47", "J44", "J9-J18", "K35", 
"K35-K38", "K55-K64", "K56", "K80-K87", "L00-L08", "M00-M25", 
"M40-M54", "M54", "M60-M79", "N10-N16", "N20-N23", "N23", "N30-N39", 
"N39", "N40-N51", "O20-O29", "R00-R09", "R06", "R07", "R07-4", 
"R10", "R10-R19", "R20-R23", "R30-R39", "R40-R46", "R50-R69", 
"R51", "R55", "S00", "S00-S09", "S01", "S06", "S20", "S20-S29", 
"S30-S39", "S40-S49", "S42", "S50-S59", "S52", "S60", "S60-S69", 
"S61", "S70-S79", "S80-S89", "S82", "S90", "S90-S99", "S91", 
"S93", "T08-T14", "T15", "T15-T19", "T63", "T66-T78", "T78", 
"T80-T88", "T83")

CodePudding user response:

We may either use do.call with grid.arrange

 do.call(grid.arrange, c(plotlist1[1], plotlist1[grep("A", namelist)]))

or with

library(ggpubr)
n1 <- 3
n2 <- 4
ggarrange(plotlist = plotlist, ncol = n1, nrow = n2)
  • Related