avoid rectangle to overlay axes

Viewed 31

Suppose data are

dat <- data.frame(x=1:9,y=2:10)

which I plot

plot(0, col="white", xlim=c(0,10), ylim=c(0,10), xaxs="i", yaxs="i", ylab="")
rect(3,0,5,10, col="gray", border=NA)
lines(dat$x, dat$y)

but the rectangle overlays the axes.

close up

I could draw them (and the box) again but I wonder whether there is a more parsimonious way of achieving the same result. I thought of subtracting a line width from the heights of the rectangle but wouldn't know how to.

1 Answers

How about using panel.first in the plot call:


dat <- data.frame(x=1:9,y=2:10)

plot(dat, type = "l", xlim=c(0,10), ylim=c(0,10), xaxs="i", yaxs="i", ylab="", 
panel.first = rect(3,0,5,10, col =  "gray", border = NA))

Created on 2020-07-08 by the reprex package (v0.3.0)

Related