[R] Conditional coloring of area between curves
Duncan Murdoch
murdoch.duncan at gmail.com
Thu Jun 6 18:45:15 CEST 2013
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