Skip to content

[Feature Request] Determine number of patches  #293

Description

@Nelson-Gon

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions