我正在 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 像素。
最佳答案
您可以执行以下操作。在第一行代码后添加 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中.
根据所需的输出更改上面代码中的宽度
和高度
。
请注意,这也适用于其他 R
图,包括 plot
、image
和 pheatmap
。
其他文件格式
除了 TIFF 之外,您还可以轻松使用other image file formats包括 JPEG、BMP 和 PNG。其中一些格式需要较少的内存来保存。
关于r - 在 R 中保存高分辨率图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38907514/