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

Resolve name conflicts: align_plots #130

Closed
clauswilke opened this issue Dec 1, 2019 · 1 comment
Closed

Resolve name conflicts: align_plots #130

clauswilke opened this issue Dec 1, 2019 · 1 comment

Comments

@clauswilke
Copy link

Both patchwork and cowplot define functions align_plots(), which fundamentally do the same thing but using quite different APIs and approaches. For the immediate future, I don't see a way of unifying them. I also think it will be quite confusing for the two packages to both define this function, since both packages will quite often be loaded at the same time (e.g., if people want to use the cowplot themes).

Since cowplot::align_plots() has been on CRAN for a while, and there's definitely code floating around using it (see e.g. here), would it be possible to rename the patchwork function and quickly re-release on CRAN? Maybe rename to justify_plots() or align_plot_panels()?

patchwork::align_plots
#> function (...) 
#> {
#>     if (is.ggplot(..1)) {
#>         plots <- list(...)
#>     }
#>     else if (is.list(..1)) {
#>         plots <- ..1
#>     }
#>     else {
#>         stop("Can only align ggplot objects or a list of them", 
#>             call. = FALSE)
#>     }
#>     lapply(plots, set_dim, get_max_dim(plots))
#> }
#> <bytecode: 0x7f9396fbbc00>
#> <environment: namespace:patchwork>

cowplot::align_plots
#> function (..., plotlist = NULL, align = c("none", "h", "v", "hv"), 
#>     axis = c("none", "l", "r", "t", "b", "lr", "tb", "tblr"), 
#>     greedy = TRUE) 
#> {
#>     plots <- c(list(...), plotlist)
#>     num_plots <- length(plots)
#>     grobs <- lapply(plots, function(x) {
#>         if (!is.null(x)) 
#>             as_gtable(x)
#>         else NULL
#>     })
#>     halign <- switch(align[1], h = TRUE, vh = TRUE, hv = TRUE, 
#>         FALSE)
#>     valign <- switch(align[1], v = TRUE, vh = TRUE, hv = TRUE, 
#>         FALSE)
#>     vcomplex_align <- hcomplex_align <- FALSE
#>     if (valign) {
#>         num_widths <- unique(lapply(grobs, function(x) {
#>             length(x$widths)
#>         }))
#>         num_widths[num_widths == 0] <- NULL
#>         if (length(num_widths) > 1 || length(grep("l|r", axis[1])) > 
#>             0) {
#>             vcomplex_align = TRUE
#>             if (axis[1] == "none") {
#>                 warning("Graphs cannot be vertically aligned unless the axis parameter is set. Placing graphs unaligned.", 
#>                   call. = FALSE)
#>                 valign <- FALSE
#>             }
#>             max_widths <- lapply(grobs, function(x) {
#>                 x$widths
#>             })
#>             if (length(grep("l", axis[1])) > 0) {
#>                 max_widths <- align_margin(max_widths, "first", 
#>                   greedy = greedy)
#>             }
#>             if (length(grep("r", axis[1])) > 0) {
#>                 max_widths <- align_margin(max_widths, "last", 
#>                   greedy = greedy)
#>             }
#>         }
#>         else {
#>             max_widths <- list(do.call(grid::unit.pmax, lapply(grobs, 
#>                 function(x) {
#>                   x$widths
#>                 })))
#>         }
#>     }
#>     if (halign) {
#>         num_heights <- unique(lapply(grobs, function(x) {
#>             length(x$heights)
#>         }))
#>         num_heights[num_heights == 0] <- NULL
#>         if (length(num_heights) > 1 || length(grep("t|b", axis[1])) > 
#>             0) {
#>             hcomplex_align = TRUE
#>             if (axis[1] == "none") {
#>                 warning("Graphs cannot be horizontally aligned unless the axis parameter is set. Placing graphs unaligned.", 
#>                   call. = FALSE)
#>                 halign <- FALSE
#>             }
#>             max_heights <- lapply(grobs, function(x) {
#>                 x$heights
#>             })
#>             if (length(grep("t", axis[1])) > 0) {
#>                 max_heights <- align_margin(max_heights, "first", 
#>                   greedy = greedy)
#>             }
#>             if (length(grep("b", axis[1])) > 0) {
#>                 max_heights <- align_margin(max_heights, "last", 
#>                   greedy = greedy)
#>             }
#>         }
#>         else {
#>             max_heights <- list(do.call(grid::unit.pmax, lapply(grobs, 
#>                 function(x) {
#>                   x$heights
#>                 })))
#>         }
#>     }
#>     for (i in 1:num_plots) {
#>         if (!is.null(grobs[[i]])) {
#>             if (valign) {
#>                 if (vcomplex_align) {
#>                   grobs[[i]]$widths <- max_widths[[i]]
#>                 }
#>                 else {
#>                   grobs[[i]]$widths <- max_widths[[1]]
#>                 }
#>             }
#>             if (halign) {
#>                 if (hcomplex_align) {
#>                   grobs[[i]]$heights <- max_heights[[i]]
#>                 }
#>                 else {
#>                   grobs[[i]]$heights <- max_heights[[1]]
#>                 }
#>             }
#>         }
#>     }
#>     grobs
#> }
#> <bytecode: 0x7f93972b9310>
#> <environment: namespace:cowplot>

Created on 2019-12-01 by the reprex package (v0.3.0)

@thomasp85
Copy link
Owner

Yes - I think the best course forward is to quickly deprecate align_plots() in patchwork in favour of another name

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

2 participants