Dear Paul, Thanks. Redrawing the points solves it for the minimal example, but what happens if you have plot(.., type="b") like below?
Actually, originally I wanted to use just grid (without mixing it with base graphics), but I couldn't find an equivalent for plot(.., type="b"). Also, later on, I would like to use eaxis() to draw nice y-axes in log-scale. I guess that's easier with gridBase then. Cheers, Marius require(grid) require(gridBase) pdf(file="Rplot.pdf", width=8, height=8, onefile=FALSE) ## set up the grid layout plot.new() # start (empty) new page with 'graphics' gl <- grid.layout(5, 5, widths=unit(c(1.8, 8, 0.8, 8, 0.8), "cm"), heights=unit(c(0.8, 8, 0.8, 8, 1.5), "cm")) pushViewport(viewport(layout=gl)) ## plot data par. <- par(no.readonly=TRUE) # save plot settings for(i in 1:2) { # rows i. <- if(i > 1) i+2 else i+1 # jumping over gaps for(j in 1:2) { # columns j. <- if(j > 1) j+2 else j+1 # jumping over gaps pushViewport(viewport(layout.pos.row=i., layout.pos.col=j.)) grid.rect(gp=gpar(col=NA, fill="gray90")) # background par(plt=gridPLT()) ## plot par(new=TRUE) # always do this before each new 'graphics' plot plot(runif(5), runif(5), type="b", log="y", xlab="", ylab="", frame.plot=FALSE, xaxt="n", yaxt="n") grid(col="white", lty="solid", lwd=1.6, equilogs=FALSE) # background grid upViewport() } } par(par.) dev.off() Paul Murrell <p...@stat.auckland.ac.nz> writes: > Hi > > On 25/09/12 11:50, Marius Hofert wrote: >> Dear Paul, >> >> Thanks for helping. Is there a way to call grid() first? The problem seems >> to be >> that everything drawn before grid() is overplotted. > > No, but you can redraw the points ... > > require(grid) > require(gridBase) > > pdf(file="Rplot.pdf", width=8, height=8, onefile=FALSE) > > ## set up the grid layout > plot.new() # start (empty) new page with 'graphics' > gl <- grid.layout(5, 5, widths=unit(c(1.8, 8, 0.8, 8, 0.8), "cm"), > heights=unit(c(0.8, 8, 0.8, 8, 1.5), "cm")) > pushViewport(viewport(layout=gl)) > > ## plot data > par. <- par(no.readonly=TRUE) # save plot settings > for(i in 1:2) { # rows > i. <- if(i > 1) i+2 else i+1 # jumping over gaps > for(j in 1:2) { # columns > j. <- if(j > 1) j+2 else j+1 # jumping over gaps > pushViewport(viewport(layout.pos.row=i., layout.pos.col=j.)) > grid.rect(gp=gpar(col=NA, fill="gray90")) # background > par(plt=gridPLT()) > ## plot > par(new=TRUE) # always do this before each new 'graphics' plot > plot(1:5, if(i==1 && j==2) c(1, 1, 2, 3, 1) else 1:5, > log="y", xlab="", ylab="", frame.plot=FALSE, xaxt="n", > yaxt="n") > # background grid > grid(col="white", lty="solid", lwd=1.6, equilogs=FALSE) > points(1:5, if(i==1 && j==2) c(1, 1, 2, 3, 1) else 1:5) > upViewport() > } > } > par(par.) > dev.off() > > Paul > >> Cheers, >> >> Marius >> >> >> >> require(grid) >> require(gridBase) >> >> pdf(file="Rplot.pdf", width=8, height=8, onefile=FALSE) >> >> ## set up the grid layout >> plot.new() # start (empty) new page with 'graphics' >> gl <- grid.layout(5, 5, widths=unit(c(1.8, 8, 0.8, 8, 0.8), "cm"), >> heights=unit(c(0.8, 8, 0.8, 8, 1.5), "cm")) >> pushViewport(viewport(layout=gl)) >> >> ## plot data >> par. <- par(no.readonly=TRUE) # save plot settings >> for(i in 1:2) { # rows >> i. <- if(i > 1) i+2 else i+1 # jumping over gaps >> for(j in 1:2) { # columns >> j. <- if(j > 1) j+2 else j+1 # jumping over gaps >> pushViewport(viewport(layout.pos.row=i., layout.pos.col=j.)) >> grid.rect(gp=gpar(col=NA, fill="gray90")) # background >> par(plt=gridPLT()) >> ## plot >> par(new=TRUE) # always do this before each new 'graphics' plot >> plot(1:5, if(i==1 && j==2) c(1, 1, 2, 3, 1) else 1:5, >> log="y", xlab="", ylab="", frame.plot=FALSE, xaxt="n", >> yaxt="n") >> grid(col="white", lty="solid", lwd=1.6, equilogs=FALSE) # >> background grid >> upViewport() >> } >> } >> par(par.) >> dev.off() >> >> >> Paul Murrell <p...@stat.auckland.ac.nz> writes: >> >>> Hi >>> >>> On 24/09/12 18:06, Marius Hofert wrote: >>>> Dear Paul, >>>> >>>> Thank you for helping. This works great. >>>> >>>> I then tried to put in a grid (via grid()). Why does that fail? >>> >>> Because grid() is used to add lines to an existing plot; just put the >>> grid() >>> call AFTER the plot() call and it should work ok. >>> >>> Paul >>> >>>> Cheers, >>>> >>>> Marius >>>> >>>> >>>> require(grid) >>>> require(gridBase) >>>> >>>> pdf(file="Rplot.pdf", width=8, height=8, onefile=FALSE) >>>> >>>> ## set up the grid layout >>>> plot.new() # start (empty) new page with 'graphics' >>>> gl <- grid.layout(5, 5, widths=unit(c(1.8, 8, 0.8, 8, 0.8), "cm"), >>>> heights=unit(c(0.8, 8, 0.8, 8, 1.5), "cm")) >>>> pushViewport(viewport(layout=gl)) >>>> >>>> ## plot data >>>> par. <- par(no.readonly=TRUE) # save plot settings >>>> for(i in 1:2) { # rows >>>> i. <- if(i > 1) i+2 else i+1 # jumping over gaps >>>> for(j in 1:2) { # columns >>>> j. <- if(j > 1) j+2 else j+1 # jumping over gaps >>>> pushViewport(viewport(layout.pos.row=i., layout.pos.col=j.)) >>>> grid.rect(gp=gpar(fill="gray90")) # background >>>> par(plt=gridPLT()) >>>> ## plot >>>> par(new=TRUE) # always do this before each new 'graphics' plot >>>> grid(col=1) >>>> plot(1:10, 1:10, log="y", xlab="", ylab="", >>>> xaxt=if(i==2) "s" else "n", yaxt=if(j==1) "s" else "n") >>>> upViewport() >>>> } >>>> } >>>> par(par.) >>>> dev.off() >>>> >>>> >>>> >>>> Paul Murrell <p...@stat.auckland.ac.nz> writes: >>>> >>>>> Hi >>>>> >>>>> On 24/09/12 09:36, Marius Hofert wrote: >>>>>> Hi, >>>>>> >>>>>> Why does the upper left panel (in the plot below) not have a gray >>>>>> background? >>>>> >>>>> That is a symptom of the conflict that occurs when the 'graphics' package >>>>> and >>>>> the 'grid' package both try to initialise a new page. >>>>> A good rule of thumb is to start a new page with 'graphics' first and >>>>> THEN add >>>>> grid' stuff ('grid' is better at sharing), so a minor adjustment to your >>>>> code >>>>> would be (#PAUL marks the changes) ... >>>>> >>>>> >>>>> pdf(file="Rplot.pdf", width=8, height=8, onefile=FALSE) >>>>> #PAUL >>>>> # Start (empty) new page with 'graphics' >>>>> plot.new() >>>>> ## set up the grid layout >>>>> gl <- grid.layout(5, 5, widths=unit(c(1.8, 8, 0.8, 8, 0.8), "cm"), >>>>> heights=unit(c(0.8, 8, 0.8, 8, 1.5), "cm")) >>>>> pushViewport(viewport(layout=gl)) >>>>> ## plot data >>>>> par. <- par(no.readonly=TRUE) # save plot settings >>>>> for(i in 1:2) { # rows >>>>> i. <- if(i > 1) i+2 else i+1 # jumping over gaps >>>>> for(j in 1:2) { # columns >>>>> j. <- if(j > 1) j+2 else j+1 # jumping over gaps >>>>> pushViewport(viewport(layout.pos.row=i., layout.pos.col=j.)) >>>>> grid.rect(gp=gpar(fill="gray90")) # background >>>>> par(plt=gridPLT()) >>>>> ## plot >>>>> #PAUL >>>>> # ALWAYS do this before each new 'graphics' plot >>>>> par(new=TRUE) >>>>> plot(1:10, 1:10, log="y", xlab="", ylab="", >>>>> xaxt=if(i==2) "s" else "n", yaxt=if(j==1) "s" else "n") >>>>> upViewport() >>>>> } >>>>> } >>>>> par(par.) >>>>> dev.off() >>>>> >>>>> >>>>> Hope that helps. >>>>> >>>>> Paul >>>>> >>>>>> Cheers, >>>>>> >>>>>> Marius >>>>>> >>>>>> >>>>>> require(grid) >>>>>> require(gridBase) >>>>>> >>>>>> pdf(file="Rplot.pdf", width=8, height=8, onefile=FALSE) >>>>>> >>>>>> ## set up the grid layout >>>>>> gl <- grid.layout(5, 5, widths=unit(c(1.8, 8, 0.8, 8, 0.8), "cm"), >>>>>> heights=unit(c(0.8, 8, 0.8, 8, 1.5), "cm")) >>>>>> if(FALSE) grid.show.layout(gl) >>>>>> pushViewport(viewport(layout=gl)) >>>>>> >>>>>> ## plot data >>>>>> par. <- par(no.readonly=TRUE) # save plot settings >>>>>> for(i in 1:2) { # rows >>>>>> i. <- if(i > 1) i+2 else i+1 # jumping over gaps >>>>>> for(j in 1:2) { # columns >>>>>> j. <- if(j > 1) j+2 else j+1 # jumping over gaps >>>>>> pushViewport(viewport(layout.pos.row=i., layout.pos.col=j.)) >>>>>> grid.rect(gp=gpar(fill="gray90")) # background >>>>>> par(plt=gridPLT()) >>>>>> ## plot >>>>>> plot(1:10, 1:10, log="y", xlab="", ylab="", >>>>>> xaxt=if(i==2) "s" else "n", yaxt=if(j==1) "s" else "n") >>>>>> par(new=TRUE) # to be run after first plot >>>>>> upViewport() >>>>>> } >>>>>> } >>>>>> par(par.) >>>>>> dev.off() >>>>>> >>>>>> ______________________________________________ >>>>>> 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. >>>>>> >>>> ______________________________________________ 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.