2016-08-11 178 views
12

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

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个像素的高度。

+0

您可能需要设置高度和宽度(和单位) –

+1

该代码适用于模拟数据!问题应该在你的情节,所以需要一个可重复的例子 – Robert

+0

同意。对我来说工作得很好 - 输出的像素尺寸是多少?当你调用'ggsave()'时,ggplot会说什么? –

回答

11

你可以使用它。在第一行代码后添加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'的图形文件保存在工作目录中。