我在R(R版本3.2.1)中创建一个散点图。我想把这个图保存为300 DPI的tiff图像,以便在期刊上发表。但是,我的代码使用gg保存或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的图形文件保存在您的工作目录中。
根据所需的输出更改上面代码中的宽度
和高度
。
请注意,这也适用于其他R
绘图,包括plot
、image
和pheatmap
。
其他文件格式
除了TIFF,您还可以轻松使用其他图像文件格式,包括JPEG、BMP和PNG。其中一些格式需要更少的内存来保存。
更简单的方法是
ggplot(data=df, aes(x=xvar, y=yvar)) +
geom_point()
ggsave(path = path, width = width, height = height, device='tiff', dpi=700)