hi: is it possible to color areas between two functions? for example, x<- 1:100; plot(x, x^2, type="l"); lines(x,0.5*x^2, type="l"); # better plotwithfill(x, x^2, 0.5*x^2, color=c("yellow", "red"); where the first color is used if f(x)=x^2 > g(x)=0.5*x^2, and the second for the reverse. Help appreciated. Regards, / ivo
Nothing easily that I can think of. You need to plot each `piece' separately using polygon(). I.e., 1. Find the `pieces'. 2. Determine the color the piece should be in. 3. Draw the piece with polygon(). Step 1 above is probably the trickiest part, but quite doable, I think. Best, Andy> From: ivo welch > > hi: is it possible to color areas between two functions? > for example, > > x<- 1:100; > plot(x, x^2, type="l"); > lines(x,0.5*x^2, type="l"); > # better > plotwithfill(x, x^2, 0.5*x^2, color=c("yellow", "red"); > > where the first color is used if f(x)=x^2 > g(x)=0.5*x^2, and > the second > for the reverse. Help appreciated. > > Regards, / ivo > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > R-project.org/posting-guide.html > >
If one function dominates the other, you can use polygon(), x <- 1:100 p <- rbind(cbind(x, 0.5*x^2), cbind(rev(x), rev(x^2))) matplot(x, cbind(x^2, 0.5*x^2), type = "l", col = 1, lty = 1) polygon(p, col = 2) or some variant of that. -roger ivo welch wrote:> > hi: is it possible to color areas between two functions? for example, > > x<- 1:100; > plot(x, x^2, type="l"); > lines(x,0.5*x^2, type="l"); > # better > plotwithfill(x, x^2, 0.5*x^2, color=c("yellow", "red"); > > where the first color is used if f(x)=x^2 > g(x)=0.5*x^2, and the second > for the reverse. Help appreciated. > > Regards, / ivo > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > R-project.org/posting-guide.html >
It could be approximated with line segments, using the segments function. Something like two calls similar to segments(x,x^2, x, 0.5*x^2) one call where f > g, the other call f < g, using a different value for the 'col' argument Make x very dense, say seq(1,100,len=1000) instead of 1:100, and it will look like it's filled. -Don At 2:16 PM -0400 4/19/04, ivo welch wrote:>hi: is it possible to color areas between two functions? for example, > > x<- 1:100; > plot(x, x^2, type="l"); > lines(x,0.5*x^2, type="l"); > # better > plotwithfill(x, x^2, 0.5*x^2, color=c("yellow", "red"); > >where the first color is used if f(x)=x^2 > g(x)=0.5*x^2, and the >second for the reverse. Help appreciated. > >Regards, / ivo > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >stat.math.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! R-project.org/posting-guide.html-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA