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

R中aplot绘制的树状图+热图组合图无法保存为PS/EPS求助

解决aplot组合图保存为PS/EPS格式的问题

问题根源

  1. 使用postscript()时未打印图也未关闭设备,导致生成空白文件
  2. ggsave()未显式指定要保存的组合图对象,默认只保存了树状图

修正方案

方案1:修复postscript保存代码

# 打开EPS设备
postscript(
  file = "heatmap.eps",
  paper = "special",
  width = 10,
  height = 20,
  horizontal = FALSE
)
# 输出组合图到设备
print(plot)
# 关闭绘图设备,完成文件写入
dev.off()

方案2:修复ggsave保存代码

显式指定要保存的组合图对象即可:

ggsave(
  filename = "dendroheatmap.ps",
  plot = plot,  # 必须指定组合图对象
  device = "ps",
  path = "/home/path/path",
  width = 12,
  height = 8,
  dpi = 300,
  units = "cm"
)

备选方案:转成gtable后保存

如果上述方法仍有问题,可将组合图转为gtable后再保存:

library(grid)
# 将aplot组合图转为gtable对象
g <- as.gtable(plot)

# 使用postscript保存
postscript("heatmap_gtable.eps", width = 10, height = 20)
grid.draw(g)
dev.off()

内容的提问来源于stack exchange,提问作者Aldo D'alessandro

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 12:32:23