[R] Conditional polygon colouring

mamuash bukana mamushbukana at gmail.com
Fri Feb 28 16:10:41 CET 2014


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?



More information about the R-help mailing list