Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Determine number of patches #293

Closed
Nelson-Gon opened this issue Feb 17, 2022 · 4 comments
Closed

[Feature Request] Determine number of patches #293

Nelson-Gon opened this issue Feb 17, 2022 · 4 comments
Labels
feature a feature request or enhancement

Comments

@Nelson-Gon
Copy link

Nelson-Gon commented Feb 17, 2022

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

@BioinformaNicks
Copy link

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.

@seb-mueller
Copy link

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

@BioinformaNicks
Copy link

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?

@seb-mueller
Copy link

Maybe that's true, which would make an even a better case for unified and straightforward to use solution..

@thomasp85 thomasp85 added the feature a feature request or enhancement label Aug 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants