Last active
August 31, 2021 22:51
-
-
Save tgirke/019d2a46ba79971e113150ecbf0eeef0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Arranging plots with patchwork. For details see this manual page: https://ggplot2-book.org/arranging-plots.html | |
## Load libraries and create some sample data | |
library(ggplot2); library(reshape2); library(patchwork) | |
iris_mean <- aggregate(iris[,1:4], by=list(Species=iris$Species), FUN=mean) | |
df_mean <- melt(iris_mean, id.vars=c("Species"), variable.name = "Samples", value.name="Values") | |
## Create lots of plots | |
p1 <- ggplot(cbind(iris, Samples="Sepal.Length"), aes(Species, Sepal.Length)) + geom_boxplot(aes(fill = Species)) + ylim(4, 8) + theme(axis.title.x=element_blank(), axis.title.y=element_blank(), legend.position = "none") + facet_grid(rows = ~Samples) + ggtitle("(A)") + theme(plot.title = element_text(hjust=-0.3)) | |
p2 <- ggplot(df_mean[df_mean$Samples=="Sepal.Width",], aes(Species, Values)) + geom_bar(position="dodge", aes(fill = Species), stat="identity") + ylim(0, 20) + theme(axis.title.x=element_blank(), axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank(), legend.position = "none") + facet_grid(rows = ~Samples) | |
p3 <- ggplot(df_mean[df_mean$Samples=="Petal.Length",], aes(Species, Values)) + geom_bar(position="dodge", aes(fill = Species), stat="identity") + ylim(0, 6) + theme(axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank(), legend.position = "none") + facet_grid(rows = ~Samples) + theme(axis.title.x = element_text(hjust=-0.2)) | |
p4 <- ggplot(df_mean[df_mean$Samples=="Petal.Width",], aes(Species, Values)) + geom_bar(position="dodge", aes(fill = Species), stat="identity") + theme(axis.title.x=element_blank(), axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank(), legend.position = "none") + facet_grid(rows = ~Samples) | |
p5 <- ggplot(df_mean[df_mean$Samples=="Sepal.Length",], aes(Species, Values)) + geom_bar(position="dodge", aes(fill = Species), stat="identity") + ylim(0, 15) + theme(axis.title.x=element_blank(), axis.title.y=element_blank(), legend.position = "none") + facet_grid(rows = ~Samples) + ggtitle("(B)") + theme(plot.title = element_text(hjust=-0.9)) | |
p6 <- ggplot(df_mean[df_mean$Samples=="Sepal.Width",], aes(Species, Values)) + geom_bar(position="dodge", aes(fill = Species), stat="identity") + ylim(0, 20) + theme(axis.title.x=element_blank(), axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank(), legend.position = "none") + facet_grid(rows = ~Samples) | |
p7 <- ggplot(df_mean[df_mean$Samples=="Petal.Length",], aes(Species, Values)) + geom_bar(position="dodge", aes(fill = Species), stat="identity") + ylim(0, 6) + theme(axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank(), legend.position = "none") + facet_grid(rows = ~Samples) + theme(axis.title.x = element_text(hjust=-0.3)) | |
p8 <- ggplot(df_mean[df_mean$Samples=="Petal.Width",], aes(Species, Values)) + geom_bar(position="dodge", aes(fill = Species), stat="identity") + theme(axis.title.x=element_blank(), axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank(), legend.position = "none") + facet_grid(rows = ~Samples) | |
p9 <- ggplot(df_mean[df_mean$Samples=="Sepal.Length",], aes(Species, Values)) + geom_bar(position="dodge", aes(fill = Species), stat="identity") + ylim(0, 15) + theme(axis.title.x=element_blank(), axis.title.y=element_blank(), legend.position = "none") + facet_grid(rows = ~Samples) + ggtitle("(C)") + theme(plot.title = element_text(hjust=-0.9)) | |
p10 <- ggplot(df_mean[df_mean$Samples=="Sepal.Width",], aes(Species, Values)) + geom_bar(position="dodge", aes(fill = Species), stat="identity") + ylim(0, 20) + theme(axis.title.x=element_blank(), axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank(), legend.position = "none") + facet_grid(rows = ~Samples) | |
p11 <- ggplot(df_mean[df_mean$Samples=="Petal.Length",], aes(Species, Values)) + geom_bar(position="dodge", aes(fill = Species), stat="identity") + ylim(0, 6) + theme(axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank(), legend.position = "none") + facet_grid(rows = ~Samples) + theme(axis.title.x = element_text(hjust=-0.3)) | |
p12 <- ggplot(df_mean[df_mean$Samples=="Petal.Width",], aes(Species, Values)) + geom_bar(position="dodge", aes(fill = Species), stat="identity") + theme(axis.title.x=element_blank(), axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank(), legend.position = "none") + facet_grid(rows = ~Samples) | |
## Lay them out with patchwork | |
( p1 + p2 + p3 + p4 + plot_layout(ncol = 4) & coord_flip() ) / | |
( ( p5 + p6 + p7 + p8 + plot_layout(ncol = 4) & coord_flip() ) | | |
( p9 + p10 + p11 + p12 + plot_layout(ncol = 4) & coord_flip() ) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment