[R] polygon

Paul Murrell p.murrell at auckland.ac.nz
Tue Apr 20 23:32:29 CEST 2004


mike.campana at freesurf.ch wrote:
> Dear all
> In order to clearly mark values wich are larger than a treshold value, I 
> would like to color the surface below the line given by plot (yy~xx). To 
> color is only the surface between abline (treshold) and yy if they are 
> larger than the specific limit. I guess I can use the function polygon, 
> but I can not find any valuable solution.
> I'm grateful to you for an advice or an example.
> Mike  
> xx <- c(1:100)
> yy <- rnorm(100)
> plot (yy~xx,type="l")
> abline (h=0.5,col="red")
> #?? how can I use polygon()
> #in order to color surface below yy value and > abline??
> polygon(xx,yy,col="gray")

You can do it by "brute force" as follows:

xx <- c(1:100)
yy <- rnorm(100)
# Set up plot but draw nothing
plot (yy ~ xx, type="n", axes=FALSE)
# Some constants
n <- 100
hline <- 0.5
# Fill in grey beneath yy
polygon(c(xx[1], xx, xx[n]), c(min(yy), yy, min(yy)), col="grey")
# Fill in white beneath abline
usr <- par("usr")
rect(usr[1], usr[3], usr[2], hline, col="white", border=NA)
# Redraw all of yy line
lines(xx, yy)
# Draw red abline
abline (h=0.5,col="red")
# Draw bounding box and axes

Or you could calculate all intercepts of yy line with the abline, but 
that's probably not worth the effort.

Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz

More information about the R-help mailing list