本文翻译自:How to save a plot as image on the disk?
I plot a simple linear regression using R. I would like to save that image as PNG or JPEG, is it possible to do it automatically? 我使用R绘制了简单的线性回归。我想将该图像另存为PNG或JPEG,是否可以自动进行? (via code) (通过代码)
There are two different questions: First, I am already looking at the plot on my monitor and I would like to save it as is. 有两个不同的问题:首先,我已经在监视器上查看该图了,我想按原样保存它。 Second, I have not yet generated the plot, but I would like to directly save it to disk when I execute my plotting code. 其次,我还没有生成绘图,但是我想在执行绘图代码时将其直接保存到磁盘。
#1楼
参考:https://stackoom.com/question/TyVi/如何将绘图作为图像保存在磁盘上
#2楼
If you use ggplot2
the preferred way of saving is to use ggsave
. 如果使用ggplot2
则首选的保存方式是使用ggsave
。 First you have to plot, after creating the plot you call ggsave
: 首先,您必须绘图,在创建绘图后,您调用ggsave
:
ggplot(...)
ggsave("plot.png")
The format of the image is determined by the extension you choose for the filename. 图像的格式取决于您为文件名选择的扩展名。 Additional parameters can be passed to ggsave
, notably width
, height
, and dpi
. 可以将其他参数传递给ggsave
,尤其是width
, height
和dpi
。
#3楼
For the first question, I find dev.print
to be the best when working interactively. 对于第一个问题,我发现dev.print
在进行交互工作时是最好的。 First, you set up your plot visually and when you are happy with what you see, you can ask R to save the current plot to disk 首先,您以视觉方式设置图表,对所看到的图像满意时,可以要求R将当前图表保存到磁盘
dev.print(pdf, file="filename.pdf");
You can replace pdf
with other formats such as png
. 您可以将pdf
替换为其他格式,例如png
。
This will copy the image exactly as you see it on screen. 这将完全按照您在屏幕上看到的图像进行复制。 The problem with dev.copy
is that the image is often different and doesn't remember the window size and aspect ratio - it forces the plot to be square by default. dev.copy
的问题在于,图像通常是不同的,并且不记得窗口的大小和纵横比-默认情况下,它会强制将dev.copy
设为正方形。
For the second question, (as others have already answered), you must direct the output to disk before you execute your plotting commands 对于第二个问题(因为其他人已经回答了),必须在执行绘图命令之前将输出定向到磁盘
pdf('filename.pdf')
plot( yourdata )
points (some_more_data)
dev.off() # to complete the writing process and return output to your monitor
#4楼
If you open a device using png()
, bmp()
, pdf()
etc. as suggested by Andrie (the best answer), the windows with plots will not pop up open, just *.png, *bmp or *.pdf files will be created. 如果您按照Andrie的建议使用png()
, bmp()
, pdf()
等打开设备(最佳答案),则不会弹出带有图的窗口,而仅* .png,* bmp或* .pdf文件将被创建。 This is convenient in massive calculations, since R can handle only limited number of graphic windows. 由于R只能处理有限数量的图形窗口,因此在进行大量计算时很方便。
However, if you want to see the plots and also have them saved, call savePlot(filename, type)
after the plots are drawn and the window containing them is active. 但是,如果要查看图并保存它们,请在绘制图并且包含它们的窗口处于活动状态后调用savePlot(filename, type)
。
#5楼
To add to these answers, if you have an R script containing calls that generate plots to screen (the native device), then these can all be saved to a pdf file (the default device for a non-interactive shell) "Rplots.pdf" (the default name) by redirecting the script into R from the terminal (assuming you are running linux or OS X), eg: 要添加到这些答案中,如果您有一个R脚本,其中包含一些调用,这些调用会生成要在屏幕上显示的图(本机设备),那么所有这些都可以保存到pdf文件(非交互式外壳的默认设备)中。 (默认名称),方法是将脚本从终端重定向到R(假设您正在运行linux或OS X),例如:
R < myscript.R --no-save
This could be converted to jpg/png as necessary 可以根据需要将其转换为jpg / png
#6楼
plotpath<- file.path(path, "PLOT_name",paste("plot_",file,".png",sep=""))
png(filename=plotpath)
plot(x,y, main= file)
dev.off()