[Feature Request] Determine number of patches
Hello,
Awesome package this! I would like to know if there could be a way to determine the number of patches of the current plot and/or access information on the layout. This would allow, among other things, looping over these patches.
My attempt at accessing these attributes
library(ggplot2)
library(patchwork)
p<-ggplot(mtcars,aes(mpg, disp)) +
geom_col()
p1 <- ggplot(mtcars,aes(disp, mpg)) +
geom_col()
p2<- p + p1
str(p2)
This contains the required information but there is no way to extract the number 2 without resorting to a RegEx based grep
A patchwork composed of 2 patches
- Autotagging is turned off
- Guides are kept
Layout:
2 patch areas, spanning 2 columns and 1 rows
t l b r
1: 1 1 1 1
2: 1 2 1 2
length on the other hand gives 10 which is not ideal.
A solution
length(p2$patches$plots)
The above however will return only one plot if the plots are identical giving an incorrect length of 1.
p3 <- p + p
length(p3$patches$plots)
Thank you very much and apologies if this is documented/or exists somewhere else already ( I could not find them).
NelsonGon
I tried length(composition_arranged$patches$layout$design) and it gave me the correct length.
Had the same issue: I did a seq_along(p3$patches$plots) only to discover that it did not count the last plot.
Just banged my head against this as well, but @BioinformaNicks solutions doesn't seem to work for me, any other suggestions?:
library(ggplot2)
library(patchwork)
p<-ggplot(mtcars,aes(mpg, disp)) +
geom_col()
p1 <- ggplot(mtcars,aes(disp, mpg)) +
geom_col()
p2<- p + p1
p2$patches$layout$design
# NULL
Just banged my head against this as well, but @BioinformaNicks solutions doesn't seem to work for me, any other suggestions?:
library(ggplot2) library(patchwork) p<-ggplot(mtcars,aes(mpg, disp)) + geom_col() p1 <- ggplot(mtcars,aes(disp, mpg)) + geom_col() p2<- p + p1 p2$patches$layout$design # NULL
Perhaps the difference is that I typically use the plot_layout function to arrange my plots, and not the simple p1+p2 syntax?
Maybe that's true, which would make an even a better case for unified and straightforward to use solution..