patchwork icon indicating copy to clipboard operation
patchwork copied to clipboard

Plot titles unaligned to their corresponding panels when title angle is higher than 0

Open leonfodoulian opened this issue 3 years ago • 0 comments

Hi,

I am facing an issue regarding the way patchwork deals with plot titles when the text angle is higher than 0. For some reason, instead of aligning the text to the corresponding panel as defined in the ggplot2 code, patchwork aligns titles to each other so that they are centered to the longest text. Having plot titles of the same size does not result in this issue. And this is a new issue I am facing, since using an older version of patchwork did not lead to this outcome. I also think this issue is linked to issue #272.

The code I used is the following:

require(ggplot2)
require(patchwork)
require(reshape2)

set.seed(seed = 123)
data(iris)
iris$Sepal.Length.x.Sepal.Width <- iris$Sepal.Length * iris$Sepal.Width
iris <- iris[,sample(x = colnames(x = iris))]

iris.melted <- reshape2::melt(data = iris,
                              id.vars = "Species")

iris.melted.l <- split(x = iris.melted,
                       f = iris.melted$variable)

reference.plot <- names(x = iris.melted.l)[1]

plots.list <- lapply(X = iris.melted.l,
                     FUN = function(variable.data,
                                    reference.plot) {
                       plot.title <- unique(x = variable.data$variable)
                       
                       out.plot <- ggplot2::ggplot(data = variable.data,
                                                   mapping = ggplot2::aes(x = value,
                                                                          y = Species,
                                                                          fill = Species)) +
                         ggplot2::geom_violin(scale = "width",
                                              show.legend = FALSE) +
                         ggplot2::labs(title = plot.title) +
                         ggplot2::theme(axis.title = ggplot2::element_blank(),
                                        axis.text.x = ggplot2::element_blank(),
                                        axis.ticks.x = ggplot2::element_blank(),
                                        plot.title = ggplot2::element_text(angle = 45,
                                                                           hjust = 0,
                                                                           vjust = 0,
                                                                           size = 10))
                       
                       if (plot.title != reference.plot) {
                         out.plot <- out.plot +
                           ggplot2::theme(axis.text.y = ggplot2::element_blank(),
                                          axis.ticks.y = ggplot2::element_blank())
                       }
                       
                       return(out.plot)
                     },
                     reference.plot = reference.plot)

combined.plots <- patchwork::wrap_plots(plots.list,
                                        nrow = 1)

The plot I got is the following: patchwork_issue_plot.pdf

Here is also my sessionInfo():

> devtools::session_info()
─ Session info ───────────────────────────────────────────────────────────────────────────────────────────────
 setting  value                       
 version  R version 3.5.0 (2018-04-23)
 os       macOS Sierra 10.12.6        
 system   x86_64, darwin15.6.0        
 ui       RStudio                     
 language (EN)                        
 collate  en_US.UTF-8                 
 ctype    en_US.UTF-8                 
 tz       Europe/Zurich               
 date     2021-09-08                  

─ Packages ───────────────────────────────────────────────────────────────────────────────────────────────────
 package     * version date       lib source        
 assertthat    0.2.1   2019-03-21 [1] CRAN (R 3.5.2)
 backports     1.1.5   2019-10-02 [1] CRAN (R 3.5.2)
 callr         3.4.3   2020-03-28 [1] CRAN (R 3.5.0)
 cli           2.0.2   2020-02-28 [1] CRAN (R 3.5.2)
 colorspace    1.4-1   2019-03-18 [1] CRAN (R 3.5.2)
 crayon        1.3.4   2017-09-16 [1] CRAN (R 3.5.0)
 desc          1.2.0   2018-05-01 [1] CRAN (R 3.5.0)
 devtools      2.3.0   2020-04-10 [1] CRAN (R 3.5.0)
 digest        0.6.25  2020-02-23 [1] CRAN (R 3.5.0)
 dplyr         0.8.5   2020-03-07 [1] CRAN (R 3.5.2)
 ellipsis      0.3.0   2019-09-20 [1] CRAN (R 3.5.2)
 fansi         0.4.1   2020-01-08 [1] CRAN (R 3.5.2)
 farver        2.0.3   2020-01-16 [1] CRAN (R 3.5.0)
 fs            1.3.2   2020-03-05 [1] CRAN (R 3.5.2)
 ggplot2     * 3.3.5   2021-06-25 [1] CRAN (R 3.5.0)
 glue          1.4.0   2020-04-03 [1] CRAN (R 3.5.0)
 gtable        0.3.0   2019-03-25 [1] CRAN (R 3.5.0)
 labeling      0.3     2014-08-23 [1] CRAN (R 3.5.0)
 lifecycle     0.2.0   2020-03-06 [1] CRAN (R 3.5.0)
 magrittr      1.5     2014-11-22 [1] CRAN (R 3.5.0)
 memoise       1.1.0   2017-04-21 [1] CRAN (R 3.5.0)
 munsell       0.5.0   2018-06-12 [1] CRAN (R 3.5.0)
 patchwork   * 1.1.1   2020-12-17 [1] CRAN (R 3.5.0)
 pillar        1.4.3   2019-12-20 [1] CRAN (R 3.5.2)
 pkgbuild      1.0.6   2019-10-09 [1] CRAN (R 3.5.2)
 pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 3.5.2)
 pkgload       1.0.2   2018-10-29 [1] CRAN (R 3.5.0)
 plyr          1.8.6   2020-03-03 [1] CRAN (R 3.5.2)
 prettyunits   1.1.1   2020-01-24 [1] CRAN (R 3.5.2)
 processx      3.4.2   2020-02-09 [1] CRAN (R 3.5.2)
 ps            1.3.2   2020-02-13 [1] CRAN (R 3.5.2)
 purrr         0.3.3   2019-10-18 [1] CRAN (R 3.5.2)
 R6            2.4.1   2019-11-12 [1] CRAN (R 3.5.2)
 Rcpp          1.0.4.6 2020-04-09 [1] CRAN (R 3.5.0)
 remotes       2.1.1   2020-02-15 [1] CRAN (R 3.5.2)
 reshape2    * 1.4.3   2017-12-11 [1] CRAN (R 3.5.0)
 rlang         0.4.11  2021-04-30 [1] CRAN (R 3.5.0)
 rprojroot     1.3-2   2018-01-03 [1] CRAN (R 3.5.0)
 rstudioapi    0.11    2020-02-07 [1] CRAN (R 3.5.2)
 scales        1.1.0   2019-11-18 [1] CRAN (R 3.5.2)
 sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 3.5.0)
 stringi       1.4.6   2020-02-17 [1] CRAN (R 3.5.0)
 stringr       1.4.0   2019-02-10 [1] CRAN (R 3.5.0)
 testthat      2.3.2   2020-03-02 [1] CRAN (R 3.5.2)
 tibble        2.1.3   2019-06-06 [1] CRAN (R 3.5.2)
 tidyselect    1.0.0   2020-01-27 [1] CRAN (R 3.5.2)
 usethis       1.6.0   2020-04-09 [1] CRAN (R 3.5.0)
 withr         2.2.0   2020-04-20 [1] CRAN (R 3.5.0)
 yaml          2.2.1   2020-02-01 [1] CRAN (R 3.5.2)

[1] /Library/Frameworks/R.framework/Versions/3.5/Resources/library

Best, Leon

leonfodoulian avatar Sep 08 '21 10:09 leonfodoulian