使用png()和dev.off()在R中打印图(lm(y〜x) [英] Print plot(lm(y~x) in R using png() and dev.off()

查看:271
本文介绍了使用png()和dev.off()在R中打印图(lm(y〜x)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印出当绘制()线性模型的拟合时R生成的回归诊断图.有四个,它们用

I'd like to print-to-file the regression diagnostic charts that R produces when you plot() the fit of a linear model. There are four, and they interrupt execution with

Hit <Return> to see next plot:
Hit <Return> to see next plot: 
Hit <Return> to see next plot: 
Hit <Return> to see next plot: 

因此,以下正常工作的代码无效:

So, the following code, which normally does work, did not:

png('Filename.png', width=mywidth, height=myheight, units='in', res=300)
plot(lm(y~x)
dev.off()

每次仍然必须按回车键,并且尚不清楚是否可以正确地对其进行子图绘制,或将每个图命名为另一个文件.

in that I still had to hit enter each time, and it isn't clear this would have properly subplotted, or named each plot as a different file.

如何捕获直接打印到磁盘的这些诊断图像?如果有关系,我在Linux机器上.

How can I capture these diagnostic images directly printed to disk? If it matters, I'm on a linux machine.

推荐答案

有两个选项,使用以下虚拟数据

A couple of options are, using the following dummy data

set.seed(42)
x <- rnorm(100)
y <- 3.4 + (0.5 * x) + rnorm(100)

使用ask参数并将其设置为FALSE:

Use the ask argument and set it to FALSE:

png('Filename%03d.png', width=6, height=6, units='in', res=300)
plot(lm(y~x), ask = FALSE)
dev.off()

请注意,我们必须使用%03d在文件名"中添加一个数字,因此对于四个图像,我们必须使用"Filename001.png"等.有关ask的详细信息,请参见?plot.lm,有关文件名中的符号,请参见?png.

Note that we have to use %03d to add a number to "Filename" so we have "Filename001.png" etc. for the four images. See ?plot.lm for the details of ask and ?png for the notation in the filename.

或者,设置具有4个面板的绘图设备并绘制模型:

Alternatively, set up the plotting device with 4 panels and plot the model:

png("Filename_all.png", width=6, height=6, units='in', res=300)
layout(matrix(1:4, ncol = 2))
plot(lm(y~x))
layout(1)
dev.off()

这篇关于使用png()和dev.off()在R中打印图(lm(y〜x)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆