使用ggdraw/plot_grid/ggsave添加多图主X轴标题时JPG丢失标题
问题描述
我用cowplot包创建2×4的多图面板,通过ggdraw()和draw_label()制作跨图的主X轴标题,再用plot_grid()把标题放在两行图形下方。简化代码如下:
# 创建轴标题对象 xTitle = ggdraw() + draw_label("lorem ipsum", fontface = "bold", hjust = 0.5) + theme(plot.margin = margin(0,0,0,7)) # 创建图形 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 # 创建图形网格 grid <- cowplot::plot_grid(p1, p2, xTitle, ncol = 1, rel_heights = c(1,1,0.2))
在RStudio中预览或用ggsave()保存为PDF时显示正常,但保存为JPG时,底部的主X轴标题会被遗漏。需要解决这个问题,或指出操作错误、提供替代方案。
会话信息:
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
解决方案
方法1:调整标题边距并指定稳定渲染设备
问题根源是标题对象的负边距导致JPG裁剪时丢失内容,同时使用ragg设备能提升文本渲染稳定性:
# 调整标题边距,避免负边距引发裁剪 xTitle = ggdraw() + draw_label("lorem ipsum", fontface = "bold", hjust = 0.5) + theme(plot.margin = margin(5, 0, 5, 0)) # 上下保留少量边距 # 生成图形网格 grid <- cowplot::plot_grid(p1, p2, xTitle, ncol = 1, rel_heights = c(1,1,0.1)) # 使用ragg设备保存JPG ggsave("plot.jpg", grid, device = ragg::agg_jpeg, width = 8, height = 6, dpi = 300)
方法2:利用plot_grid()的bottom参数直接添加标题
cowplot的plot_grid()支持通过bottom参数直接挂载底部标题,无需单独创建标题对象,能规避裁剪问题:
# 先组合两行图形 row_plots <- cowplot::plot_grid(p1, p2, ncol = 1, rel_heights = c(1,1)) # 添加底部主X轴标题 grid <- cowplot::plot_grid(row_plots, bottom = ggdraw() + draw_label("lorem ipsum", fontface = "bold")) # 保存JPG ggsave("plot.jpg", grid, width = 8, height = 6, dpi = 300)
方法3:改用patchwork包实现多图布局
patchwork对跨图标题的支持更直观,渲染稳定性优于当前方案:
library(patchwork) # 组合图形并添加底部标题 grid <- (p1 / p2) + plot_annotation(caption = "lorem ipsum", theme = theme(plot.caption = element_text(hjust = 0.5, fontface = "bold", size = 12))) # 保存JPG ggsave("plot.jpg", grid, width = 8, height = 6, dpi = 300)
内容的提问来源于stack exchange,提问作者llewmills
相关产品推荐
相关产品推荐

