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

Marius Hofert marius.hofert at math.ethz.ch
Sun Sep 23 23:36:08 CEST 2012


Hi,

Why does the upper left panel (in the plot below) not have a gray background?

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()



More information about the R-help mailing list