简体   繁体   English

将多个glm图导出为PNG?

[英]Exporting multiple glm plots as PNGs?

SO, 所以,

I'm trying to export plots of my linear model. 我正在尝试导出我的线性模型的图。 When I do it as a PDF, the PDF has four pages and four different charts. 当我以PDF格式化时,PDF有四页和四个不同的图表。 When I export as a PNG, I only get the first chart. 当我作为PNG导出时,我只获得第一张图表。 How do I export so that I get all four graphs as separate PNG files? 如何导出以便将所有四个图形作为单独的PNG文件?

What worked with the PDF: 什么适用于PDF:

lrfitX11SUMS <- glm(X11SUMS ~ POLITA + CIRCULATION + COMPAPER + TrafficRankUS, data=NewspTest)

    summary(lrfitOTONE)
    pdf("/Users/william/Desktop/output/lmfitOTONE1.pdf")
    plot(lrfitOTONE)
    dev.off()

What DIDN'T WORK with PNG (and spent two hours digging around on the internet and in the plot documentation to no avail): 什么不适用于PNG(并花了两个小时在互联网和情节文件挖掘无济于事):

lrfitX11SUMS <- glm(X11SUMS ~ POLITA + CIRCULATION + COMPAPER + TrafficRankUS, data=NewspTest)

summary(lrfitOTONE)
png("/Users/william/Desktop/output/lmfitOTONE1.png", width=720, height=720, pointsize=16)
png("/Users/william/Desktop/output/lmfitOTONE2.png", width=720, height=720, pointsize=16)
png("/Users/william/Desktop/output/lmfitOTONE3.png", width=720, height=720, pointsize=16)
png("/Users/william/Desktop/output/lmfitOTONE4.png", width=720, height=720, pointsize=16)
plot(lrfitOTONE)
dev.off()

How do I get my images? 我如何获取图像?

Thanks, 谢谢,

-Wm -wm

A PDF allows multipage documents. PDF允许多页文档。 A PNG image is fundamentally incompatible with this idea. PNG图像从根本上与这个想法不相容。 Reading ?png and appreciating the need to look at the filename argument would have directed you to ?postscript for the details. 阅读?png并认识到需要查看filename参数会引导您到?postscript获取详细信息。

You want something like: 你想要的东西:

png("/Users/william/Desktop/output/lmfitOTONE%1d.png", width=720, 
    height=720, pointsize=16)
plot(lrfitOTONE)
dev.off()

where the %1d in the filename is a wildcard that expands to a 1 digit numeric value such that you get four figures with the names you wanted. 文件名中的%1d是一个通配符,扩展为1位数字值,这样您就可以得到四个带有所需名称的数字。 Your 4 calls to png() set up four separate devices, only the latter of which was used and subsequently closed, the other three remained open. 你对png() 4次调用设置了四个独立的设备,只有后者使用并随后关闭,其他三个仍保持打开状态。

Something like this: 像这样的东西:

setwd("/Users/william/Desktop/output/")
tmpf <- function(i) {
   png(paste("lmfitOTONE",i,".png",sep=""), width=720, height=720, pointsize=16)
}
wplot <- c(1,2,3,5) ## see ?plot.lm for definition of 'which'
for (i in seq_along(wplot)) {
   tmpf(i); plot(lrfitOTONE, which=wplot[i]); dev.off()
}

The key is realizing that plot.lm (which is the method used by plot applied to a glm object, because glm is a subclass of lm and doesn't have its own particular plot method) displays diagnostic plots based on the which argument, and the default value of which is the same as wplot above. 关键是要实现plot.lm (这是应用于glm对象的plot所使用的方法,因为glmlm的子类,并且没有自己特定的绘图方法)根据which参数显示诊断图,以及的默认值which是相同的wplot上方。 So basically: figure out how to create the individual sub-plots. 所以基本上:弄清楚如何创建各个子图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM