Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting multiple glm plots as PNGs?

Tags:

plot

r

png

glm

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

like image 394
user1017124 Avatar asked Nov 29 '11 21:11

user1017124


1 Answers

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.

like image 62
Gavin Simpson Avatar answered Sep 22 '22 19:09

Gavin Simpson