On Fri, Oct 19, 2012 at 6:28 PM, Ali Tofigh <alix.tof...@gmail.com> wrote:
> On Wed, Oct 17, 2012 at 4:08 PM, ilai <ke...@math.montana.edu> wrote: > > On Wed, Oct 17, 2012 at 11:10 AM, Ali Tofigh <alix.tof...@gmail.com> > wrote: > > ## this works as intended with a mix of plot.new() and grid.newpage > pdf("test3.pdf") > plot.new(); my.plot(); grid.newpage(); my.plot() > dev.off() > > Ahh. The evolution of problems in R-help threads...what a way to start the week... While grid and base don't play nice, that doesn't mean they *can't* be mixed through proper use of gridBase - which requires some trial and error on your part to see what works and what doesn't. For example this also works without explicit calls to newpage: my.plot <- function(){ plot(1, type='n', axes=FALSE, bty='n', xlab='', ylab='') # a blank base plot grid.rect(c(0.25, 0.75), width=c(0.5, 0.5), gp=gpar(fill=c("blue", "red")), vp=viewport(w=unit(.5, "npc"),just='left',clip='on')) # note the clipping pushViewport(viewport(w=unit(.5, "npc"),just='right')) par(fig=gridFIG(), new=TRUE) plot(1:10) popViewport() } pdf(file='test4.pdf') my.plot() ; my.plot() # onefile = TRUE by default dev.off() What's going on here ? well, maybe some grid gurus more knowledgeable than I can add insight here but I think you are mixing different issues. The example you gave, test1 didn't work because when you call par(new=T) the first time, there is still no plot "to go on". Same "issue" as produces the warning graphics.off() par(new=T) Modify my.plot() so the grid.rect is drawn *after* the plot(1:10), your test1.pdf is correct with just a blank first page (from the "extra" call to par). Thus, new.page() in your test3 solved this annoying issue of the first par(new=T) call - which implies you can also draw the grid grobs first, but I would not call this "a mix of new.page and grid.newpage". In the future, to get the most out of R-help my unsolicited advice is to remember you are the only one who knows how much / what help you need - and set up your question / reproducible code accordingly. We the list readers don't know your level, what things you tried etc. The original example made no mention of base graphics, and the second reply didn't give any hints as to how/why mixing base and grid matters here. Cheers > /ali > > > > > require(gridBase) > > pdf("test.pdf") > > > > grid.rect(gp = gpar(fill="blue")) > > grid.newpage() > > grid.rect(gp=gpar(fill='blue')) > > # mix in base+grid. adapted from ?gridPAR in gridBase > > par(fig=gridFIG(), new=TRUE) > > plot(1:10) > > # plot.new() # uncomment to see it's unnecessary > > plot(1:10) > > pushViewport(viewport(width=0.5, height=0.5)) ; > > grid.rect(gp=gpar(col="grey", lwd=2)) > > plot(rnorm(10)) > > grid.newpage() > > grid.rect(gp=gpar(fill='blue')) > > dev.off() > > > >> > >> I use grid as much as possible, but for example for plotting > >> dendrograms, I don't know how to plot them other than using base > >> graphics. So I use the functions in gridBase to produce those plots. > > > > > > Then you may have noticed the dendrogram examples in the gridBase docs > don't > > use plot.new() either but use lattice for the layout. > > > > > >> In order to do that I have to call plot.new() at some point in my code > >> to initialize the base graphics, and that can mess things up. > > > > > >> > >> No. See example above or provide a minimal reproducible example that > does > >> require it. > > > > > > > >> > >> /ali > >> > >> > >> On Tue, Oct 9, 2012 at 4:00 PM, Greg Snow <538...@gmail.com> wrote: > >> > The plot.new function is for base graphics and base and grid graphics > >> > don't usually play well together. You probably want to use > >> > grid.newpage function instead. > >> > > >> > On Tue, Oct 9, 2012 at 1:26 PM, Ali Tofigh <alix.tof...@gmail.com> > >> > wrote: > >> >> Hi, > >> >> > >> >> when using the grid package, I've come across this weird behaviour > >> >> where a call to plot.new() will start a new page for a multi-page > pdf, > >> >> but then the margins will somehow behave strangely for all but the > >> >> first page: here is some code: > >> >> > >> >> pdf("test.pdf"); plot.new(); grid.rect(gp = gpar(fill="blue")); > >> >> plot.new(); grid.rect(gp = gpar(fill="blue")); dev.off() > >> >> > >> >> The first page is filled completely with a blue rectangle, but in the > >> >> second page, the margins clip the rectangle. This is causing me > >> >> considerable headache, as I rely on many grid functions for plotting. > >> >> This seems like a bug to me, or is there something about the > behaviour > >> >> of plot.new() and/or grid functions that I don't understand? > >> >> > >> >> /Ali > >> >> > >> >> ______________________________________________ > >> >> R-help@r-project.org mailing list > >> >> https://stat.ethz.ch/mailman/listinfo/r-help > >> >> PLEASE do read the posting guide > >> >> http://www.R-project.org/posting-guide.html > >> >> and provide commented, minimal, self-contained, reproducible code. > >> > > >> > > >> > > >> > -- > >> > Gregory (Greg) L. Snow Ph.D. > >> > 538...@gmail.com > >> > >> ______________________________________________ > >> R-help@r-project.org mailing list > >> https://stat.ethz.ch/mailman/listinfo/r-help > >> PLEASE do read the posting guide > >> http://www.R-project.org/posting-guide.html > >> and provide commented, minimal, self-contained, reproducible code. > > > > > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.