Created
August 3, 2018 20:37
-
-
Save grimbough/8294b06d0dd047248394fb3f5940bc3d to your computer and use it in GitHub Desktop.
Demonstrating laying out plots in a 'tree' structure using gridExtra
This file contains 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
library(ggplot2) | |
library(gridExtra) | |
## create 17 plots, where the only element is a number in the centre | |
res <- list() | |
for(i in 1:17) { | |
x <- data.frame(x = 1, y = 1, num = as.character(i)) | |
res[[i]] <- ggplot(x, aes(x = x, y = y, label = num)) + | |
geom_text() + | |
theme_void() + | |
NULL | |
} | |
## create a heirarchical layout matrix | |
## there's probably a more elegant way of doing this | |
nrow <- ceiling(log2( length(res) + 1 )) | |
ncol <- 2^(nrow-1) | |
layout <- matrix(0, nrow = nrow, ncol = ncol) | |
for(i in 1:nrow) { | |
j <- i-1 | |
vals <- (2^j):(2^(j+1)-1) | |
layout[i,] <- rep( vals, each = ncol/length(vals) ) | |
} | |
## plot using our layout matrix | |
grid.arrange(grobs=res[1:17] , layout_matrix = mat) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment