将多个 glm 图导出为 PNG?

发布于 2024-12-18 19:06:39 字数 996 浏览 2 评论 0原文

所以,

我正在尝试导出线性模型的图。当我将其制作为 PDF 时,PDF 有四页和四个不同的图表。当我导出为 PNG 时,我只得到第一个图表。如何导出以便将所有四个图表作为单独的 PNG 文件获取?

什么适用于 PDF:

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

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

什么不适用于 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()

如何获取我的图像?

谢谢,

-Wm

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. When I export as a PNG, I only get the first chart. How do I export so that I get all four graphs as separate PNG files?

What worked with the 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):

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请爱~陌生人 2024-12-25 19:06:39

PDF 允许多页文档。 PNG 图像从根本上与这个想法不兼容。阅读 ?png 并意识到需要查看 filename 参数,将会引导您前往 ?postscript 了解详细信息。

您想要类似的内容:

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

其中文件名中的 %1d 是一个通配符,它​​扩展为 1 位数字值,这样您就可以得到带有您想要的名称的四个数字。您对 png() 的 4 次调用设置了四个单独的设备,只有后者被使用并随后关闭,其他三个保持打开状态。

A PDF allows multipage documents. A PNG image is fundamentally incompatible with this idea. Reading ?png and appreciating the need to look at the filename argument would have directed you to ?postscript for the details.

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. 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.

只是在用心讲痛 2024-12-25 19:06:39

像这样的事情:

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()
}

关键是实现 plot.lm (这是 plot 使用的方法应用于 glm 对象,因为 glmlm 的子类,没有自己特定的绘图方法)根据 which 参数显示诊断图,默认值为whichwplot 相同 多于。所以基本上:弄清楚如何创建各个子图。

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. So basically: figure out how to create the individual sub-plots.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文