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

Modify display order of ggplots

I am displaying several ggplots into a ggarrange matrix.
ggarrange displays each subsequent plot one by one by column and starts with a new row when the last column is reached.

What I would like to do is to display each plot by row then start with a new column when the last row is reached.

Here is an example

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

##### Import libraries
library(ggplot2)
library(ggpubr)

##### Initiating objects
set.seed(1)
df <- data.frame(matrix(rnorm(100), 25, 4))
listggplots <- list()
k <- 0
vecColors <- rep(c("#21b78c", "#21a0b7", "#2143b7", "#b521b7"), each=4)

##### Filling list of ggplots
for(i in 1:ncol(df)){
  for(j in 1:ncol(df)){
    k <- k + 1
    dfTemp <- df[, c(i, j)]
    colnames(dfTemp) <- c("var1", "var2")
    listggplots[[k]] <- ggplot(dfTemp, aes(var1, var2)) + 
      geom_point(size=10, color=vecColors[k]) + 
      scale_x_continuous(name=colnames(df)[i]) + 
      scale_y_continuous(name=colnames(df)[j]) + 
      theme(legend.position="none", axis.title=element_text(size=12))
  }
}

##### Display plots
ggarrange(plotlist=c(listggplots), nrow=4, ncol=4)

enter image description here

As we can see, the current matrix order for displaying plots is:

     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8
[3,]    9   10   11   12
[4,]   13   14   15   16

I would like each column to have only one ggplot color.
So the order of the matrix would be:

     [,1] [,2] [,3] [,4]
[1,]    1    5    9   13
[2,]    2    6   10   14
[3,]    3    7   11   15
[4,]    4    8   12   16

I found the following topic but it is manual disposition of plots and I would like something automatic.
ggarrange: combine multiple plots

Thank you for any idea

>Solution :

From the manual, ggarrange is just a wrapper.

ggarrange {ggpubr}

Arrange multiple ggplots on the same page. Wrapper around plot_grid(). …

The original plot_grid has byrow argument:

cowplot::plot_grid(plotlist = listggplots, byrow = FALSE, nrow = 4, ncol = 4)

enter image description here

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