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

Common border for patchwork #201

Closed
suragnair opened this issue Jul 3, 2020 · 5 comments
Closed

Common border for patchwork #201

suragnair opened this issue Jul 3, 2020 · 5 comments

Comments

@suragnair
Copy link

suragnair commented Jul 3, 2020

Thanks for the lovely tool! I would like to have a single outline border for a patchwork.

using the & theme(...) option as suggested in #106 does not seem to work. Instead it applies a border on each individual plot.

(ggplot() + ggplot()) & theme(panel.border = element_rect(colour = "black", fill=NA))
Screenshot 2020-07-03 at 1 37 49 AM

How can obtain this:
Screenshot 2020-07-03 at 1 37 00 AM

> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-conda_cos6-linux-gnu (64-bit)
Running under: Ubuntu 18.04.4 LTS

Matrix products: default
BLAS/LAPACK: /users/surag/anaconda3/envs/r36_cran/lib/libopenblasp-r0.3.9.so

Random number generation:
 RNG:     Mersenne-Twister 
 Normal:  Inversion 
 Sample:  Rounding 
 
locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] patchwork_1.0.1 ggplot2_3.3.2  

loaded via a namespace (and not attached):
 [1] withr_2.2.0      dplyr_1.0.0      crayon_1.3.4     grid_3.6.3      
 [5] R6_2.4.1         lifecycle_0.2.0  gtable_0.3.0     magrittr_1.5    
 [9] scales_1.1.1     pillar_1.4.4     rlang_0.4.6      generics_0.0.2  
[13] vctrs_0.3.1      ellipsis_0.3.1   glue_1.4.1       purrr_0.3.4     
[17] munsell_0.5.0    compiler_3.6.3   pkgconfig_2.0.3  colorspace_1.4-1
[21] tidyselect_1.1.0 tibble_3.0.1    
@raulfernandezn
Copy link

Please check this possible solution:

library(patchwork)
library(ggplot2)
library(grid)
library(gridExtra)

#Plot
p <- (ggplot() + ggplot())

#Add border
p
grid.rect(width = 0.98, height = 0.98, gp = gpar(lwd = 3, col = "black", fill = NA))

It will produce this:

imagen

@suragnair
Copy link
Author

suragnair commented Jul 30, 2020

Thanks! The solution works for this particular case. However, would it be possible to compose the output with other plots- say if I wanted a 2x2 grid of each of the above (2 plots inside a shared border)?

@thomasp85
Copy link
Owner

library(ggplot2)
library(patchwork)
theme_border <- theme_gray() + 
  theme(plot.background = element_rect(fill = NA, colour = 'black', size = 3))
ggplot() + 
  ggplot() + 
  plot_annotation(theme = theme_border)

Created on 2020-11-06 by the reprex package (v0.3.0)

@suragnair
Copy link
Author

Thanks, unfortunately this can not be composed further.

(ggplot() + 
  ggplot() + 
  plot_annotation(theme = theme_border) ) |
(ggplot() + 
  ggplot() + 
  plot_annotation(theme = theme_border) )

gives
image

@simschul
Copy link

simschul commented Jan 3, 2024

This should do what you want:

wrap_elements(full = ggplot() + ggplot() + plot_annotation(theme = theme_border)) |
  wrap_elements(full = ggplot() + ggplot() + plot_annotation(theme = theme_border))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants