0

With the following lines of code I product a graph:

chol <- read.table(url("http://assets.datacamp.com/blog_assets/chol.txt"), header = TRUE)
ggplot(data=chol, aes(chol$AGE)) + 
    geom_histogram(breaks=seq(20, 50, by =2), 
                   col="red", 
                   fill="green")

with the same way I produce 6 plots.

How is it possible to have this six plot in the same image in the first line have the 3 and in the second above the first have the other 3?

zx8754
  • 52,746
  • 12
  • 114
  • 209
PitterJe
  • 216
  • 2
  • 12

2 Answers2

4

grid.extra does this job

library(ggplot2)
library(gridExtra)

chol <- read.table(url("http://assets.datacamp.com/blog_assets/chol.txt"), header = TRUE)
p1 <- p2 <- p3 <- p4 <- p5<- p6 <- ggplot(data=chol, aes(chol$AGE)) + 
  geom_histogram(breaks=seq(20, 50, by =2), 
                 col="red", 
                 fill="green")

grid.arrange(p1, p2, p3, p4, p5, p6, ncol=3)

enter image description here

J_F
  • 9,956
  • 2
  • 31
  • 55
2

Another way to do this is using cowplot.

library(cowplot)
plot_grid(p1,p2,p3,p4,p5,p6, ncol = 3, align = "v")

enter image description here

You can also adjust the height of each row if you would like using rel_heights inside the plot_gridcommand. Row 1 will be half the height of row 2.

plot_grid(p1,p2,p3,p4,p5,p6, ncol = 3, align = "v", rel_heights = c(1,2))

enter image description here

Or you can adjust the width of each column using the rel_widths. Column 1 will be half the width of columns 2 and 3

plot_grid(p1,p2,p3,p4,p5,p6, ncol = 3, align = "v", rel_widths = c(1,2,2))

enter image description here

TheSciGuy
  • 1,154
  • 11
  • 22