[R] colour code areas of a plot
Olivier Crouzet
olivier.crouzet at univ-nantes.fr
Tue Oct 22 17:14:12 CEST 2013
On Tue, 22 Oct 2013 16:43:18 +0200
Martin Batholdy <batholdy at googlemail.com> wrote:
Hi,
you should replace your "color names" by calls to the rgb() function in
order to generate the adequate colors, something like:
bg_colors <- c(rep (rgb (0,1,0),20), rep (rgb (0,1,1),10), rep (rgb
(0,1,0),20), rep(rgb (1,0,0),5), rep(rgb (1,1,0),45))
(though it should obviously be automatically extracted from your data I
suppose).
Then it is possible to use the color vector as an option to
any plotting command e.g.:
plot(color_scheme$t, color_scheme$t,col=bg_colors)
or (something like):
rect(xleft = color_scheme$t[1:99], ybottom = -1, xright = color_scheme$t
[2:100], ytop = 1, col=bg_colors, lwd=0)
Olivier.
> 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!
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html and provide commented,
> minimal, self-contained, reproducible code.
--
Olivier Crouzet, PhD
Laboratoire de Linguistique -- EA3827
Université de Nantes
Chemin de la Censive du Tertre - BP 81227
44312 Nantes cedex 3
France
phone: (+33) 02 40 14 14 05 (lab.)
(+33) 02 40 14 14 36 (office)
fax: (+33) 02 40 14 13 27
e-mail: olivier.crouzet at univ-nantes.fr
http://www.lling.univ-nantes.fr/
More information about the R-help
mailing list