The simplest way is to use clip():
> clip(0, 33, -1, 0)
> polygon(ff,gg,col=2)
> clip(0, 33, 0, 1)
> polygon(ff,gg,col=4)
-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352
-----Original Message-----
From: r-help-bounces at r-project.org
[mailto:r-help-bounces at r-project.org] On Behalf Of mamuash
bukana
Sent: Friday, February 28, 2014 9:11 AM
To: r-help at r-project.org
Subject: [R] Conditional polygon colouring
Dear R users
I want to paint parts of a polygon with different colours
conditional
to values of the variable.
Example:
f<-seq(1,33)
g<-sin(f)
i<-rep(0,33)
ff<-c(f,rev(f))
gg<-c(g,rev(i))
plot(ff,gg,type="n")
polygon(ff,gg,col=2) # but I wanted to plot areas below and
above the
x-axis with different colours.
In this attempt, I tried the following:
g2<-array(NA,33)
for(j in 1:33){
g2[j]<-ifelse(g[j]<=0,0,g[j])
}
gg2<-c(g2,i)
plot(ff,gg,type="n")
polygon(ff,gg,col=2)
lines(polygon(ff,gg2,col=4)) # but in this plot there are many
overlaps on the x-axis when 0 is substituted for negative g
values.
May someone suggest me how to plot the two regions (above and
below
x-axis) without affecting the actual plot region with different
colours please?
______________________________________________
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.