wilkelab / cowplot

cowplot: Streamlined Plot Theme and Plot Annotations for ggplot2
https://wilkelab.org/cowplot/
704 stars 84 forks source link

Hidden legend shown as an empty space when aligning plots by left/right axis in a 2*2 array #148

Closed yanxianl closed 5 years ago

yanxianl commented 5 years ago

Hi, I was trying to assemble plots in a 2*2 array. However, aligning plots by left and right axis brought out the hiden legend as an empty space, which couldn't be removed by reducing the margin of the plot. Aligning plot by top and bottom axis was fine, though.

library(dplyr)
library(ggplot2)
library(cowplot)

mtcars <- mtcars %>%
  mutate(cyl = gsub(4, "a really long legend: level-1", cyl), 
         cyl = gsub(6, "a really long legend: level-2", cyl), 
         cyl = gsub(8, "a really long legend: level-3", cyl))

p1 <- ggplot(mtcars, aes(x = qsec, y = disp)) + 
  geom_point(aes(colour = cyl)) + 
  facet_wrap(~gear) + 
  theme_cowplot() +
 # try to reduce the empty space in the middle by setting right margin to -1 cm
  theme(plot.margin = margin(r = -1, unit = "cm"),
        legend.position = "none")

p2 <- ggplot(mtcars, aes(disp, mpg)) + 
  geom_point(aes(colour = cyl)) + 
  theme_cowplot()

p3 <- ggplot(mtcars, aes(qsec, mpg)) +
  geom_point(aes(colour = vs)) + 
  theme_cowplot()

# Align by top and bottom axis: OK
plot_grid(p1, p2, p1, p3, labels = "AUTO", ncol = 2, align = 'h', 
          axis = "tb", rel_widths = c(1, 1, 1, 1)) 

Figure 2-tb


# Align by left and right axis: the legend of p1 produces a gap in the middle
plot_grid(p1, p2, p1, p3, labels = "AUTO", ncol = 2, align = 'v', 
          axis = "lr", rel_widths = c(1, 1, 1, 1)) 

Figure 2-merged

Created on 2019-09-08 by the reprex package (v0.3.0)

Session info ``` r devtools::session_info() #> - Session info ---------------------------------------------------------- #> setting value #> version R version 3.6.1 (2019-07-05) #> os Windows 10 x64 #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate English_United States.1252 #> ctype English_United States.1252 #> tz Europe/Berlin #> date 2019-09-08 #> #> - Packages -------------------------------------------------------------- #> package * version date lib source #> assertthat 0.2.1 2019-03-21 [1] CRAN (R 3.6.1) #> backports 1.1.4 2019-04-10 [1] CRAN (R 3.6.0) #> callr 3.3.1 2019-07-18 [1] CRAN (R 3.6.1) #> cli 1.1.0 2019-03-19 [1] CRAN (R 3.6.1) #> colorspace 1.4-1 2019-03-18 [1] CRAN (R 3.6.1) #> cowplot * 1.0.0 2019-07-11 [1] CRAN (R 3.6.1) #> crayon 1.3.4 2017-09-16 [1] CRAN (R 3.6.1) #> curl 4.0 2019-07-22 [1] CRAN (R 3.6.1) #> desc 1.2.0 2018-05-01 [1] CRAN (R 3.6.1) #> devtools 2.1.0 2019-07-06 [1] CRAN (R 3.6.1) #> digest 0.6.20 2019-07-04 [1] CRAN (R 3.6.1) #> dplyr * 0.8.3 2019-07-04 [1] CRAN (R 3.6.1) #> evaluate 0.14 2019-05-28 [1] CRAN (R 3.6.1) #> fs 1.3.1 2019-05-06 [1] CRAN (R 3.6.1) #> ggplot2 * 3.2.1 2019-08-10 [1] CRAN (R 3.6.1) #> glue 1.3.1 2019-03-12 [1] CRAN (R 3.6.1) #> gtable 0.3.0 2019-03-25 [1] CRAN (R 3.6.1) #> highr 0.8 2019-03-20 [1] CRAN (R 3.6.1) #> htmltools 0.3.6 2017-04-28 [1] CRAN (R 3.6.1) #> httr 1.4.1 2019-08-05 [1] CRAN (R 3.6.1) #> knitr 1.24 2019-08-08 [1] CRAN (R 3.6.1) #> labeling 0.3 2014-08-23 [1] CRAN (R 3.6.0) #> lazyeval 0.2.2 2019-03-15 [1] CRAN (R 3.6.1) #> magrittr 1.5 2014-11-22 [1] CRAN (R 3.6.1) #> memoise 1.1.0 2017-04-21 [1] CRAN (R 3.6.1) #> mime 0.7 2019-06-11 [1] CRAN (R 3.6.0) #> munsell 0.5.0 2018-06-12 [1] CRAN (R 3.6.1) #> pillar 1.4.2 2019-06-29 [1] CRAN (R 3.6.1) #> pkgbuild 1.0.4 2019-08-05 [1] CRAN (R 3.6.1) #> pkgconfig 2.0.2 2018-08-16 [1] CRAN (R 3.6.1) #> pkgload 1.0.2 2018-10-29 [1] CRAN (R 3.6.1) #> prettyunits 1.0.2 2015-07-13 [1] CRAN (R 3.6.1) #> processx 3.4.1 2019-07-18 [1] CRAN (R 3.6.1) #> ps 1.3.0 2018-12-21 [1] CRAN (R 3.6.1) #> purrr 0.3.2 2019-03-15 [1] CRAN (R 3.6.1) #> R6 2.4.0 2019-02-14 [1] CRAN (R 3.6.1) #> Rcpp 1.0.2 2019-07-25 [1] CRAN (R 3.6.1) #> remotes 2.1.0 2019-06-24 [1] CRAN (R 3.6.1) #> rlang 0.4.0 2019-06-25 [1] CRAN (R 3.6.1) #> rmarkdown 1.14 2019-07-12 [1] CRAN (R 3.6.1) #> rprojroot 1.3-2 2018-01-03 [1] CRAN (R 3.6.1) #> scales 1.0.0 2018-08-09 [1] CRAN (R 3.6.1) #> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.6.1) #> stringi 1.4.3 2019-03-12 [1] CRAN (R 3.6.0) #> stringr 1.4.0 2019-02-10 [1] CRAN (R 3.6.1) #> testthat 2.2.1 2019-07-25 [1] CRAN (R 3.6.1) #> tibble 2.1.3 2019-06-06 [1] CRAN (R 3.6.1) #> tidyselect 0.2.5 2018-10-11 [1] CRAN (R 3.6.1) #> usethis 1.5.1 2019-07-04 [1] CRAN (R 3.6.1) #> withr 2.1.2 2018-03-15 [1] CRAN (R 3.6.1) #> xfun 0.8 2019-06-25 [1] CRAN (R 3.6.1) #> xml2 1.2.2 2019-08-09 [1] CRAN (R 3.6.1) #> yaml 2.2.0 2018-07-25 [1] CRAN (R 3.6.0) #> #> [1] C:/Users/ljt89/Documents/R/win-library/3.6 #> [2] C:/Program Files/R/R-3.6.1/library ```
clauswilke commented 5 years ago

Yes, that's how it works. You can work around it by aligning plots manually, outside of plot_grid(), as explained here: https://wilkelab.org/cowplot/articles/aligning_plots.html