Skip to content

area between two curves, but one is not continuous

2 messages · Anton Meyer, Gabor Grothendieck

#
Hello,

I want to colorize the area between two curves, but one of these
curves isn't continuous.

The best solution I found is the 2nd example in the help of polygon,
but how can I get no area filling for the missing data in the 2nd curve.

example:

x1 = c(1:8)
x2 = c(1:8)
y1 = c(1,5,6,1,4,5,5,5)
y2 = c(0,3,3,NA,NA,1,3,4)

plot(x1,y1,type="l")
lines(x2,y2)

for the missing parts I want no filling.

so for this examples the code would be:
polygon(c(1:3,3:1),c(y1[1:3],rev(y2[1:3])),col="green")
polygon(c(6:8,8:6),c(y1[6:8],rev(y2[6:8])),col="green")

How can I generalize this for a longer curve with more data?

AxM
#
If you don't need borders on the polygons then it can be simply done two points
at a time checking that neither point is an NA:

# data
x1 <- x2 <- 1:8
y1 <- c(1,5,6,1,4,5,5,5)
y2 <- c(0,3,3,NA,NA,1,3,4)

# plot
plot(x1,y1,type="l")
lines(x2,y2)

# fill in area between curves with green two points at a time
for(i in seq(2, length(x1)))
   if (!any(is.na(y2[c(i-1, i)])))
      polygon(c(x1[i-1], x1[i], x2[i], x2[i-1]),
         c(y1[i-1], y1[i], y2[i], y2[i-1]),
         col = "green", border = 0)
On 9/7/06, Anton Meyer <axmeyer at googlemail.com> wrote: