Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

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

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading