[R] Conditional coloring of area between curves

Roland Pape roland.pape at uni-oldenburg.de
Fri Jun 7 11:51:10 CEST 2013


Hi Duncan,

that's exactly what I was looking for! The series have common times, so that's no problem. Thanks a lot!

Roland
________________________________________
Von: Duncan Murdoch [murdoch.duncan at gmail.com]
Gesendet: Donnerstag, 6. Juni 2013 18:45
An: Roland Pape
Cc: r-help at r-project.org
Betreff: Re: [R] Conditional coloring of area between curves

On 06/06/2013 10:41 AM, Roland Pape wrote:
> Dear list,
>
> I have two time series of temperatures from different sites plotted in the same diagram and would like to color the area between the curves differently, dependent on whether site 1 is cooler than site 2 (colored let's say in blue) or warmer (red). While polygone() works fine to color the enclosed area in general, I'm struggling with this conditional coloring. Any help is greatly appreciated!

Suppose the series are named "site1" and "site2", and they have common
times in a variable "times".  Then the following should do what you want:

smaller <- pmin(site1, site2)
plot(x, site1, ylim = range(c(site1, site2)), type="n")
polygon(c(x, rev(x)), c(smaller, rev(site1)), col="red")
polygon(c(x, rev(x)), c(smaller, rev(site2)), col="blue")

If the times for the two series are different it's a little harder;
first you need to give them common times, and that will depend on how
you decide to evaluate the values between observations. Probably linear
interpolation (using approx()) is fine, but it's up to you.

Duncan Murdoch



More information about the R-help mailing list