Yacine Hajji
Yacine Hajji

Reputation: 1449

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

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

Upvotes: 1

Views: 679

Answers (3)

chomeyama
chomeyama

Reputation: 96

The plots can be ordered easily by

n <- ncol(df)
plts <- listggplots[(1:(n^2)) %>% matrix(ncol=n) %>% t %>% as.vector]
ggarrange(plotlists=plts, nrow=n, ncol=n)

Upvotes: 0

zx8754
zx8754

Reputation: 56044

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

Upvotes: 3

Hansel Palencia
Hansel Palencia

Reputation: 1036

This is straightforward by switching the color mapping in your rep() function and switching the naming conventions of the i and j in the scale_x/y_continuous()

See below

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"), 4)

##### Filling list of ggplots
for(i in 1:ncol(df)){
  for(j in 1:ncol(df)){
    k <- k + 1
    dfTemp <- df[, c(j, i)]
    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)[j]) + 
      scale_y_continuous(name=colnames(df)[i]) + 
      theme(legend.position="none", axis.title=element_text(size=12))
  }
}

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

enter image description here

Update Based on Comment ------

If you want to keep the axis order as above, then don't change the i and j positions.



##### 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"), 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

Upvotes: 1

Related Questions