Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using cowplot in R to make a ggplot chart occupy two consecutive rows

Tags:

r

ggplot2

cowplot

This is my code:

library(ggplot2)
library(cowplot)


df <- data.frame(
  x = 1:10, y1 = 1:10, y2 = (1:10)^2, y3 = (1:10)^3, y4 = (1:10)^4
)

p1 <- ggplot(df, aes(x, y1)) + geom_point()
p2 <- ggplot(df, aes(x, y2)) + geom_point()
p3 <- ggplot(df, aes(x, y3)) + geom_point()
p4 <- ggplot(df, aes(x, y4)) + geom_point()
p5 <- ggplot(df, aes(x, y3)) + geom_point()
# simple grid
plot_grid(p1, p2, 
          p3, p4,
          p5, p4)

But I don't want to repeat p4 I want to "stretch" p4 to occupy col2 and rows 2 and 3.

Any help?

like image 484
Laura Avatar asked Mar 09 '26 08:03

Laura


1 Answers

You may find this easier using gridExtra::grid.arrange().

library(gridExtra)

grid.arrange(p1, p2, p3, p4, p5, 
             ncol = 2, 
             layout_matrix = cbind(c(1,3,5), c(2,4,4)))

Result:

enter image description here

like image 95
neilfws Avatar answered Mar 13 '26 22:03

neilfws



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!