Skip to content
Prev 306176 / 398506 Next

Background color in a grid plot seems to interfere with plot()

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. 

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 <paul at stat.auckland.ac.nz> writes: