[R] colour code areas of a plot

Martin Batholdy batholdy at googlemail.com
Tue Oct 22 16:43:18 CEST 2013


Hi,

I would like to colour different areas of a plot.
But I don't know how to do this efficiently.

As an example;

lets say three stimuli were presented in an experiment, alternating, one at a time.
Now I want to plot time on the x-axis and the plot-area should colour code the stimulus that was presented at that time interval
(green for stimulus 1, yellow for stimulus 2 etc.)


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 color_scheme data-frame and without having to manually draw all the rectangles as in the example above?



thanks for any suggestions!


More information about the R-help mailing list