[R] colour code areas of a plot

Ken Knoblauch ken.knoblauch at inserm.fr
Tue Oct 22 17:08:49 CEST 2013


Martin Batholdy <batholdy <at> googlemail.com> writes:
> I would like to colour different areas of a plot.
> But I don't know how to do this efficiently.
> 
> here an example:
> (t = time)
> 
> t <- 1:100
> bg_colors <- c(rep('green',20), rep('yellow',10), 
rep('green',20), rep('red',5),
> rep('yellow',45)) 
> 
> color_scheme <- data.frame(t, bg_colors)
> 
> plot(c(), c(), xlim = c(1,100), ylim=c(-1,1))
> 
> rect(xleft = 1, ybottom = -1, xright = 20, ytop = 1, 
col = 'green', lwd=0) 
> rect(xleft = 20, ybottom = -1, xright = 30, ytop = 1, 
col = 'yellow', lwd=0) 
>> 
> now how can I do this efficiently based on the c
olor_scheme data-frame
 and without having to manually draw
> all the rectangles as in the example above?

The first 4 arguments of rect can be vectors 
as can be the col argument.  So you might be able to
draw all of the regions with a single call to rect.
I've done this to create alternating light and dark
regions to highlight condition changes.
See ?rect, of course.

> 
> thanks for any suggestions!
> 

-- 
Kenneth Knoblauch
Inserm U846
Stem-cell and Brain Research Institute
Department of Integrative Neurosciences
18 avenue du Doyen Lépine
69500 Bron
France
tel: +33 (0)4 72 91 34 77
fax: +33 (0)4 72 91 34 61
portable: +33 (0)6 84 10 64 10
http://www.sbri.fr/members/kenneth-knoblauch.html



More information about the R-help mailing list