Search code examples
rggplot2cowplot

Adding a master x-axis title to a multipanel grid of plots using ggdraw(), plot_grid() and ggsave() works in pdf but not jpg


I am creating a large 2 x 4 multi-graph panel using the packages described in the title. I have created a separate object as a master x-axis title spanning the graphs using ggdraw() and draw_label() from the cowplot package, which I have placed underneath two rows of graphs using plot_grid(). I have created a simplified version of this using toy data below

# create the axis title object
xTitle = ggdraw() +
           draw_label("lorem ipsum",
                      fontface = "bold",
                      hjust = 0.5) +
           theme(plot.margin = margin(0,0,0,7))

# create graphs
ggplot(data = iris,
       mapping = aes(x = Sepal.Width,
                     y = Sepal.Length)) +
       geom_point() -> p1

ggplot(data = iris,
       mapping = aes(x = Sepal.Width,
                     y = Sepal.Length)) +
       geom_point() -> p2

# create the grid 
grid <- cowplot::plot_grid(p1, p2, xTitle, ncol = 1, rel_heights = c(1,1,0.2))

Now when you print this object in rstudio...

grid

enter image description here

...or as a pdf...

# save it as pdf
ggplot2::ggsave(filename = "panel.pdf",
                plot = grid,
                device = "pdf",
                dpi = "retina", 
                width = 30,
                height = 15,
                units = "cm")

...it looks greatenter image description here

However when you save it as a jpg it excludes the bottom object of the grid

# save it as jpg
ggplot2::ggsave(filename = "panel.jpg",
                plot = grid,
                device = "jpg",
                dpi = "retina", 
                width = 30,
                height = 15,
                units = "cm")

enter image description here

Does anyone know how to print the jpg so it doesn't exclude the bottom object, or give advice about what I'm doing wrong or offer an alternative way of achieving the same result?

Here is my session info

R version 4.2.2 (2022-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22621)

Matrix products: default

locale:
[1] LC_COLLATE=English_Australia.utf8  LC_CTYPE=English_Australia.utf8   
[3] LC_MONETARY=English_Australia.utf8 LC_NUMERIC=C                      
[5] LC_TIME=English_Australia.utf8    

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

other attached packages:
 [1] multcomp_1.4-20 TH.data_1.1-1   MASS_7.3-58.1   survival_3.4-0  mvtnorm_1.1-3  
 [6] cowplot_1.1.1   emmeans_1.8.0   nlme_3.1-159    forcats_0.5.2   stringr_1.4.1  
[11] dplyr_1.0.10    purrr_0.3.4     readr_2.1.2     tidyr_1.2.0     tibble_3.1.8   
[16] ggplot2_3.3.6   tidyverse_1.3.2

loaded via a namespace (and not attached):
 [1] lubridate_1.8.0     lattice_0.20-45     zoo_1.8-10          digest_0.6.29      
 [5] assertthat_0.2.1    utf8_1.2.2          R6_2.5.1            cellranger_1.1.0   
 [9] backports_1.4.1     reprex_2.0.2        coda_0.19-4         httr_1.4.4         
[13] pillar_1.8.1        rlang_1.0.6         googlesheets4_1.0.1 readxl_1.4.1       
[17] rstudioapi_0.14     Matrix_1.4-1        labeling_0.4.2      textshaping_0.3.6  
[21] splines_4.2.2       googledrive_2.0.0   munsell_0.5.0       broom_1.0.1        
[25] compiler_4.2.2      modelr_0.1.9        systemfonts_1.0.4   pkgconfig_2.0.3    
[29] tidyselect_1.2.0    codetools_0.2-18    fansi_1.0.3         crayon_1.5.1       
[33] tzdb_0.3.0          dbplyr_2.2.1        withr_2.5.0         grid_4.2.2         
[37] jsonlite_1.8.0      xtable_1.8-4        gtable_0.3.1        lifecycle_1.0.3    
[41] DBI_1.1.3           magrittr_2.0.3      scales_1.2.1        estimability_1.4.1 
[45] cli_3.3.0           stringi_1.7.8       farver_2.1.1        fs_1.5.2           
[49] xml2_1.3.3          ragg_1.2.2          ellipsis_0.3.2      generics_0.1.3     
[53] vctrs_0.4.1         sandwich_3.0-2      tools_4.2.2         glue_1.6.2         
[57] hms_1.1.2           colorspace_2.0-3    gargle_1.2.0        rvest_1.0.3        
[61] haven_2.5.1 

Solution

  • Works as expected if you open and close the jpeg device, e.g.

    library(tidyverse)
    library(cowplot)
    #> 
    #> Attaching package: 'cowplot'
    #> The following object is masked from 'package:lubridate':
    #> 
    #>     stamp
    # create the axis title object
    xTitle = ggdraw() +
      draw_label("lorem ipsum",
                 fontface = "bold",
                 hjust = 0.5) +
      theme(plot.margin = margin(0,0,0,7))
    
    # create graphs
    ggplot(data = iris,
           mapping = aes(x = Sepal.Width,
                         y = Sepal.Length)) +
      geom_point() -> p1
    
    ggplot(data = iris,
           mapping = aes(x = Sepal.Width,
                         y = Sepal.Length)) +
      geom_point() -> p2
    
    # create the grid 
    grid <- cowplot::plot_grid(p1, p2, xTitle, ncol = 1, rel_heights = c(1,1,0.2))
    
    jpeg(filename = "test.jpg", width = 30, height = 15, units = "cm", res = 300)
    grid
    dev.off()
    #> quartz_off_screen 
    #>                 2
    

    Created on 2023-03-14 with reprex v2.0.2

    image.jpeg


    Does that approach work on your system? Or do you still 'lose' the bottom title?