Home > Mobile >  r ggplot2 with marrange: how to display the charts row-wise instead of column wise
r ggplot2 with marrange: how to display the charts row-wise instead of column wise

Time:06-30

marrangeGrob apparently displays the charts by column first. How to arrange these charts row-wise instead?

library(gridExtra)
library(tidyr)
library(ggplot2)
dat <- c(2,3,4,5,6,7,8,9,0,1)
time <- 1:10
data <- tibble(dat, time)
p1 <- ggplot(data, aes(x= time, y=dat))  
  geom_point(color="orange")
p2 <- ggplot(data, aes(x= time, y=dat))  
  geom_point(color="blue")
clist <- list(p1,p2)
marrangeGrob(clist, nrow=2, ncol=2, as.table=F )

enter image description here

CodePudding user response:

Using the layout_matrix argument you could specify to add the plots by row (byrow=TRUE):

library(gridExtra)
library(ggplot2)
dat <- c(2,3,4,5,6,7,8,9,0,1)
time <- 1:10
data <- data.frame(dat, time)
p1 <- ggplot(data, aes(x= time, y=dat))  
  geom_point(color="orange")
p2 <- ggplot(data, aes(x= time, y=dat))  
  geom_point(color="blue")
clist <- list(p1,p2)

marrangeGrob(clist, nrow=2, ncol=2, 
             layout_matrix = matrix(seq_len(4), nrow = 2, byrow = TRUE))

  • Related