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] Align panels without aligning axis titles #348

Open
selkamand opened this issue Jan 21, 2024 · 0 comments
Open

[Feature Request] Align panels without aligning axis titles #348

selkamand opened this issue Jan 21, 2024 · 0 comments

Comments

@selkamand
Copy link

selkamand commented Jan 21, 2024

Thanks so much for your work on this incredible package!

As I use patchwork more and more, I've noticed the most common change I make to patchwork plots is 'un-aligning' axis titles to remove large amounts of unnecessary white space (see image below)

annotely_image

It would be hugely helpful to have the option of not aligning axis titles to one another

This feature was mentioned previously #110 (comment) but I'm hoping you might give an update on the likelihood of this feature being added?

Why not just use an existing workaround?
While there are several ways to nudge an axis title to the right, it often requires playing around in an interactive session to figure out an appropriate amount of nudging, which is mildly inconvenient for one plot. The real issue, however, is that when building shareable functions and packages for creating complex plots (e.g. oncoplots / annotated heatmaps / etc.) - it is difficult to pick sensible defaults for 'axis title nudging' since titles & axis-text length may vary based on data supplied. The result is uglier default plots and more work for end-users since without sensible defaults these functions must expose 'nudging' paramaters and basically forward the problem along.

Possible Solutions in Order of Preference

  1. Ability to decouple axis title alignment from plot panel alignment (as described in the first image)
  2. A workaround robust to different axis text lengths and number of composed plots

If this feature won't ever make it to patchwork, and no simple workaround is available: would you have any advice for how a motivated party might work towards creating one (e.g. is directly querying/calculating y-axis text widths from each ggplot axis to calculate appropriate amounts to nudge titles across possible to implement in a robust manner)

Thanks again for all your work on this package! It's a huge contribution to R

Reprex

library(ggplot2)
library(tibble)
library(patchwork)

df <- mtcars |>
  rownames_to_column('car') |>
  as_tibble()

gg1 = df |>
  ggplot(aes(y=wt, x=car)) +
  geom_col() +
  theme_bw() +
  xlab(NULL) +
  ylab('Plot 1') +
  theme(axis.text.x = element_blank())
   #theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))


gg2 = df |>
   ggplot(aes(y="number_of_cylinders", x=car, fill = as.character(cyl))) +
   geom_tile(width = 0.9) +
  ylab('Plot 2') +
  theme_bw() +
   theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))

gg1 / gg2

sessionInfo()
#> R version 4.3.2 (2023-10-31)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Pop!_OS 22.04 LTS
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0 
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
#> 
#> 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       
#> 
#> time zone: Australia/Sydney
#> tzcode source: system (glibc)
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] patchwork_1.2.0.9000 tibble_3.2.1         ggplot2_3.4.4       
#> 
#> loaded via a namespace (and not attached):
#>  [1] gtable_0.3.4      dplyr_1.1.4       compiler_4.3.2    reprex_2.1.0     
#>  [5] tidyselect_1.2.0  scales_1.3.0      yaml_2.3.8        fastmap_1.1.1    
#>  [9] R6_2.5.1          labeling_0.4.3    generics_0.1.3    knitr_1.45       
#> [13] munsell_0.5.0     R.cache_0.16.0    pillar_1.9.0      R.utils_2.12.3   
#> [17] rlang_1.1.3       utf8_1.2.4        xfun_0.41         fs_1.6.3         
#> [21] cli_3.6.2         withr_3.0.0       magrittr_2.0.3    digest_0.6.34    
#> [25] grid_4.3.2        rstudioapi_0.15.0 lifecycle_1.0.4   R.methodsS3_1.8.2
#> [29] R.oo_1.25.0       vctrs_0.6.5       evaluate_0.23     glue_1.7.0       
#> [33] farver_2.1.1      styler_1.10.2     fansi_1.0.6       colorspace_2.1-0 
#> [37] rmarkdown_2.25    purrr_1.0.2       tools_4.3.2       pkgconfig_2.0.3  
#> [41] htmltools_0.5.7

Created on 2024-01-21 with reprex v2.1.0

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

1 participant