【问题标题】:Saving a high resolution image in R在 R 中保存高分辨率图像
【发布时间】:2016-12-18 20:25:29
【问题描述】:

我正在使用 R(R 版本 3.2.1)中的 ggplot 创建散点图。我想将图形保存为 300 DPI 的 tiff 图像,以便在期刊上发表。但是,我使用 ggsave 或 tiff() 和 dev.off 的代码似乎不起作用,只能将其保存为 96 DPI。任何帮助将不胜感激!!下面是我使用这两种方法的代码示例:

library(ggplot2)

x <- 1:100
y <- 1:100

ddata <- data.frame(x,y)

library(ggplot2)

#using ggsave
ggplot(aes(x, y), data = ddata) +
  geom_point() +
  geom_smooth(method=lm, fill = NA, fullrange=TRUE, color = "black")

ggsave("test.tiff", units="in", width=5, height=4, dpi=300, compression = 'lzw')

#using tiff() and dev.off
tiff('test.tiff', units="in", width=5, height=4, res=300, compression = 'lzw')

ggplot(aes(x, y), data = ddata) +
  geom_point() +
  geom_smooth(method=lm, fill = NA, fullrange=TRUE, color = "black")

dev.off()

输出是 96 DPI,宽度为 1500 像素,高度为 1200 像素。

【问题讨论】:

  • 您可能需要设置高度和宽度(和单位)
  • 该代码适用于模拟数据!问题应该出在您的情节上,因此需要一个可重现的示例
  • 同意。对我来说很好——你的输出的像素尺寸是多少,当你打电话给 ggsave() 时 ggplot 说什么?

标签: r ggplot2 publish tiff


【解决方案1】:

您可以执行以下操作。在第一行代码之后添加您的 ggplot 代码,并以 dev.off() 结尾。

tiff("test.tiff", units="in", width=5, height=5, res=300)
# insert ggplot code
dev.off()

res=300 指定您需要分辨率为 300 dpi 的图形。名为“test.tiff”的图形文件保存在your working directory

根据所需的输出更改上述代码中的widthheight

请注意,这也适用于其他 R 图,包括 plotimagepheatmap

其他文件格式

除了 TIFF,您还可以轻松使用 other image file formats,包括 JPEG、BMP 和 PNG。其中一些格式需要较少的内存来保存。

【讨论】:

    【解决方案2】:

    更简单的方法是

    ggplot(data=df, aes(x=xvar, y=yvar)) + 
    geom_point()
    
    ggsave(path = path, width = width, height = height, device='tiff', dpi=700)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-03
      • 2015-10-20
      • 1970-01-01
      • 2018-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多