[R] Problems with "pdf" device using "plot" "glht" function on "multcomp" library.
Paul Johnson
pauljohn32 at gmail.com
Sun Sep 19 00:28:45 CEST 2010
Hi, Kenneth
It is not clear if you mean that your pdf output usually works, but it
does not in this special case, or that this is a first effort with
pdf. The answer might depend on which is the case case.
If you are just getting started, can I refer you to some lecture notes
I made about saving graphs in R?
http://pj.freefaculty.org/R/SummerCamp2010/PJ-Lectures/plotting2.pdf
If you cut off the file name at the end, you will see the list of the
other lectures I prepared for our university's "Summer Stats Camp" in
2010.
If pdf does usually work, Could I suggest you take this code and
rearrange? Where you have this:
pdf("plot1.pdf")
m1<-aov(Nitratos~Descripcion-1,data=Sx)
vect1<-table(Sx$Descripcion)
K<-contrMat(vect1,base=4)
dnk<-glht(m1,linfct=K)
summary(dnk)
old.par<-par(no.readonly = TRUE)
par(mai=c(1,2,1.25,1),mgp=c(3,1,0))
print(plot(dnk,las=1,xlab=""))
print(abline(v=0,lty=2))
par(old.par)
dev.off()
Instead, separate the "stats" part from the plot part
m1 <- aov(Nitratos~Descripcion-1, data=Sx)
vect1 <- table(Sx$Descripcion)
K <- contrMat(vect1, base=4)
dnk <- glht(m1, linfct=K)
summary(dnk)
pdf("plot1.pdf", onefile=F, paper="special", height=6, width=6)
### old.par<-par(no.readonly = TRUE)
par(mai = c(1, 2, 1.25, 1), mgp = c(3,1,0))
plot(dnk, las = 1, xlab = "")
abline(v = 0, lty = 2)
####par(old.par)
dev.off()
I've made changes to cut your "print" statements and moved your pdf
command down to "sandwich" the plot commands. I've added some
embellishment which I almost guarantee will make your pdf output more
useful. (Spaces are inserted around <- and after commas for
readability. Check the R examples, they generally recommend that).
To the R experts, it may be that the "par" stuff seems obvious, but to
the rest of us, well, it is not completely so. You do not need to
save the par values because the par setting you change is instantly
forgotten when you do dev.off(). So you don't need to worry about
saving the default settings and then returning them. Run par() to see
for yourself after dev.off(). par's defaults will be back where they
were at the beginning.
You would only need to do "remember my par" thing if you were trying
to put several graphs into a single output device, which you aren't.
And I've made that clear by putting onefile=F in the pdf statement.
I'd try it without the "par" at all if there is still trouble in the
output file.
I added the paper="special" option in the pdf command in order to make
the margins in the output more reasonable. If you don't do that, the
pdf output assumes you are wanting a whole sheet of paper, so there
will be some massive margins in your output.
Good luck.
--
Paul E. Johnson
Professor, Political Science
1541 Lilac Lane, Room 504
University of Kansas
More information about the R-help
mailing list