Search code examples
rggplot2subplot

Arrange a list of plots and multiple lines of text using ggarrange or grid.arrange


I would like to arrange a list of plots and several lines of text in one graph using grid.arrange or ggarrange. Following is what I have tried, but I prefer to use grid.arrange(grobs = plot_list, ncol = 2) combined with text, instead of typing plots in the list one by one, if possible. (Refer to : How do I arrange a variable list of plots using grid.arrange?) However, so far I haven't found a way to do it yet.

library(datasets)
library(ggplot2)
library(grid)
library(gridExtra)

d <- swiss[1:21, ]

my_list <- list()
for(i in 1:ncol(d)) {            
  my_list[[i]] <- d[ ,i]
}

d_plot <- data.frame(sj = 2000:2020, my_list)
names(d_plot) <- c("sj", "A", "B", "C", "D", "E", "F") 

plot_function <- function(d_plot, y){ 
  p <- ggplot(d_plot,aes_string(x = "sj", y = y)) + 
    geom_line() 
    return(p)
}

plot_list <- list()
indicators <- colnames(d_plot)[2:15]


for (i in 1:6){
  y <- indicators[i]
  plot_list[[i]] <- plot_function(d_plot, y)
  }

text <- textGrob("BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA 
                 BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA 
                 BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA 
                 BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA ")
grid.arrange(plot_list[[1]],plot_list[[2]],plot_list[[3]],plot_list[[4]],
             plot_list[[5]],plot_list[[6]],text,ncol=4)

Also, how can I make the text "BLA BLA BLA" take up the last two blocks instead of just one, as well as left align the text, so that it looks better?

Thank you in advance!


Solution

  • You can control the layout using the layout_matrix argument.

    Combining the text grob in the plot_list enables all the elements of the list to be plotted using the list object rather than having to type out each element of the list.

    To control the layout of long text input requires the text grob to be modified as follows:

    a) position the text grob to the left of the viewport with x = unit(0.10, "npc") this says the x coordinate for the text is 10% along the x axis (npc = normalised parent coordinate) b) left align the text with hjust = 0, c) vertical alignment of the text can be adjusted likewise with y = ... and vjust = .... if you want to.

    You will probably need to adjust the values according to your use case and the visual effect you are happy with, this may also include adjusting text size which can be done in the text grob gp = gpar(cex = ...).

    library(datasets)
    library(ggplot2)
    library(grid)
    library(gridExtra)
    
    
    text <- textGrob(label = paste("BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA",
                                   "BLA BLA BLA BLA BLA BLA BLA BLA ",
                                   "BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA",
                                   "BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA BLA",
                                   "BLA BLA BLA BLA", sep = "\n"),
                     x = unit(0.10, "npc"),
                     hjust = 0)
    
    
    plot_list[[7]] <- text
    
    grid.arrange(grobs = plot_list,
                 layout_matrix = matrix(c(1, 2, 3, 4, 5, 6, 7, 7),
                                        nrow = 2,
                                        byrow = TRUE)
                 )
    

    Created on 2023-06-28 with reprex v2.0.2