You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用ggsave()多列保存绘图时,2/3内容被截断的问题

问题描述

我需要将200个3行1列的patchwork绘图对象(总计200×3幅图)排列成3行200列,保存到单页PDF中。当前代码无报错,但PDF中仅能看到最下方的绘图,上方两幅被截断。调整width/height、修改列数(如每页50列)、设置nrow参数均无法解决问题。单独保存每个patchwork对象时显示正常。

重现代码

example1 <- function(x)
{
Plot1 <- ggplot(iris[x,], aes(x= Species, y=Sepal.Length))+
  geom_point()

Plot2 <- ggplot(iris[x,], aes(x= Species, y=Sepal.Width))+
  geom_point()

Plot3 <- ggplot(iris[x,], aes(x= Species, y=Petal.Length))+
  geom_point()

Plot1 / Plot2 / Plot3
}

vec <- c(1:150)

ListP <- lapply(vec, example1)

ggsave("test.pdf", arrangeGrob(grobs = ListP, ncol=150), device = "pdf",
       width = 1500, height = 20,
       units = c("cm"), dpi = 300, limitsize = FALSE)
解决方案

问题出在arrangeGrob与patchwork对象的兼容性上——patchwork本身已经定义了内部的垂直布局(Plot1 / Plot2 / Plot3),用arrangeGrob嵌套时会打乱布局计算,导致上方绘图被挤压截断。改用patchwork原生的wrap_plots函数即可解决:

修改后的代码

library(patchwork)

# 生成绘图列表(和原代码一致)
example1 <- function(x)
{
Plot1 <- ggplot(iris[x,], aes(x= Species, y=Sepal.Length))+
  geom_point() + theme(plot.margin = margin(0,0,0,0)) # 确保边距为0

Plot2 <- ggplot(iris[x,], aes(x= Species, y=Sepal.Width))+
  geom_point() + theme(plot.margin = margin(0,0,0,0))

Plot3 <- ggplot(iris[x,], aes(x= Species, y=Petal.Length))+
  geom_point() + theme(plot.margin = margin(0,0,0,0))

Plot1 / Plot2 / Plot3
}

vec <- c(1:150)
ListP <- lapply(vec, example1)

# 用wrap_plots排列,指定列数,patchwork会自动处理内部布局
combined_plot <- wrap_plots(ListP, ncol = 150)

# 保存PDF,调整尺寸适配布局
ggsave("test_fixed.pdf", combined_plot, device = "pdf",
       width = 1500, height = 20,
       units = "cm", dpi = 300, limitsize = FALSE)

关键说明

  • wrap_plots是patchwork专为多个patchwork/ggplot对象设计的布局函数,能正确识别每个patchwork内部的布局结构,避免挤压截断。
  • 确保每个子图的边距设为margin(0,0,0,0),减少不必要的空白占用空间,避免布局溢出。
  • 如果需要更精细的布局控制,可以通过wrap_plots的heights参数统一设置每个patchwork对象的高度比例,比如heights = rep(c(1,1,1), 150)(对应每个patchwork里的3个子图高度一致)。

内容的提问来源于stack exchange,提问作者MrPotato

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 20:21:07