在R中保存高分辨率图像 [英] Saving a high resolution image in R
问题描述
我在R(R版本3.2.1)中使用ggplot创建scatterplot。我想将图形保存为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()和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 =黑色)
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'的图形文件保存在您的工作目录中。
I'm creating a scatterplot using ggplot in R (R version 3.2.1). I want to save the graph as a tiff image in 300 DPI in order to publish it in a journal. However, my code using ggsave or tiff() with dev.off doesn't seem to work and only saves it in 96 DPI. Any help would be greatly appreciated!! Below is a sample of my code using both methods:
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()
The output is a 96 DPI with a width of 1500 pixels and a height of 1200 pixels.
You can use this. Add your ggplot code after the first line of code and end with dev.off()
.
tiff('test.tiff', units="in", width=5, height=5, res=300)
#insert ggplot code
dev.off()
res=300
specifies that you need a figure with a resolution of 300 dpi. The figure file named 'test.tiff' is saved in your working directory.
这篇关于在R中保存高分辨率图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!