Similar questions have been asked many times, for example here and here. However, all of the other answers I have seen so far don't really solve my problem.
I am trying to plot multiple different sized plots in the same window. I am working with tidygraph
objects and using ggraph
to create the plots. I tried using gridExtra
and cowplots
, however, I cant seem to get them to work as I want.
For example, to begin I create some data, turn them into tidygraph
objects and put them together in a list, like so:
library(tidygraph)
library(ggraph)
library(cowplot)
library(gridExtra)
# create some data
edges <- data.frame(from = c(1,1), to = c(2,3))
edges1 <- data.frame(from = c(1, 2, 2, 1, 5, 5), to = c(2, 3, 4, 5, 6, 7))
edges2 <- data.frame(from = c(1,2,2,1), to = c(2,3,4,5))
tg <- tbl_graph(edges = edges)
tg_1 <- tbl_graph(edges = edges1)
tg_2 <- tbl_graph(edges = edges2)
myList <- list(tg, tg_1, tg_2)
Then I create a plotting function using ggraph
and create a list of plots, like so:
# create plotting function
plotFun <- function(List, n) {
plot <- ggraph(List, "partition") +
geom_node_tile(aes(fill = "red"), size = 0.25) +
scale_y_reverse() +
theme_void() +
theme(legend.position = "none")
}
# create list of all plots
allPlots <- lapply(myList, plotFun, n = length(myList))
So, in my example, I have 3 different plots... and I am trying to plot all 3 in the same window, but each plot has a different size. My desired output is shown in the image below...
What I was trying to do is have one plot "full-sized", the 2nd plot, 1/2 the size of the first plot, and the third plot 1/4 the size of the first plot. That would look something like:
NOTE: the image was made in photoshop, so the scales I mentioned above aren't exact in the image.
My ideas at an attempted solution were to use either gridExtra
or cowplot
... I have included some basic attempts, but they don't work... they seem to change the column or row size rather than the actual plot size:
Attemped Solutions
# ATTEMPED COWPLOT SOLUTION
plot_grid(plotlist = allPlots, align = "hv", ncol = 2, rel_heights = c(1, 1/2, 1/4), rel_widths = c(1, 1/2, 1/4))
# ATTEMPED GRIDEXTRA SOLUTION
grid.arrange(
grobs = allPlots,
widths = c(1, 0.5, 1/4),
heights = c(1, 0.5, 1/4),
layout_matrix = rbind(c(1, 2, 3))
)
EDIT -- fixed to shrink third chart per OP.
library(patchwork)
design <- c( # we specify the top, left, bottom, and right
area(1, 1, 4, 4), # coordinates for each of the three plots
area(1, 5, 2, 6),
area(3, 5)
)
allPlots[[1]] + allPlots[[2]] + allPlots[[3]] +
plot_layout(design = design)