[R] background color

Gabor Grothendieck ggrothendieck at gmail.com
Tue Apr 17 14:21:34 CEST 2007

On 4/17/07, yannig goude <yanniggoude at yahoo.fr> wrote:
>  I want to add different colors on the background of a classical plot. Each color is associated to an interval of the x axis.
>  example: the background is red on the interval [1,10], blue on [11,20].
>  I try the rect function but it isn't appropriate for the background..

You can use rect together with par("usr") like this:

usr <- par("usr")
plot(1:20, type = "n")
rect(1, usr[3], 10, usr[4], col = "red")
rect(11, usr[3], 20, usr[4], col = "blue")

There is also an example of this using polygon in place of rect here:


and an example of doing it with lattice graphics using xyplot.zoo
(the same idea would work with xyplot) mid way through the examples in:


More information about the R-help mailing list