[R] Filled region on graph

Yves Gauvreau cyg at sympatico.ca
Fri Nov 3 03:23:19 CET 2000


I knew about polygon() and I was wondering if there where a high level
solution to do this sort of things.

Many thanks to all for a great peice of software.

Yves Gauvreau

----- Original Message -----
From: "Ross Ihaka" <ihaka at stat.auckland.ac.nz>
To: "Yves Gauvreau" <cyg at sympatico.ca>
Cc: <r-help at stat.math.ethz.ch>
Sent: Thursday, November 02, 2000 9:09 PM
Subject: Re: [R] Filled region on graph


> On Thu, Nov 02, 2000 at 07:27:14PM -0500, Yves Gauvreau wrote:
> > Hi,
> >
> > Say I'd like to fill in color or by hashing the region above 0.5 and
> > below -0.5 in the following situation.
> >
> > > x <- seq(0,2*pi, length=100)
> > > plot(x, sin(x), type="l")
> > > # Now fill the top part and the bottom part
>
> Like many other problems there is no high level solution and you have
> to create your own.  The function "polygon" can be used to draw filled
> polygons and it can be used to fill a polygon approximation to the area
> you want filled.  (Hashing is on my TODO list, but its not close to
> the top).
>
> To start, we need the points where the line y==0.5 cuts the curve.
> In the case you mention, it is easy to see that these points are
>
>     lower <- asin(0.5)
>     upper <- pi - asin(0.5).
>
> More generally, you could determine the intersection points by root
finding
>
>     lower <- uniroot(function(x) sin(x)-0.5, lower=0, upper=pi/2)$root
>     upper <- uniroot(function(x) sin(x)-0.5, lower=pi/2, upper=pi)$root
>
> Now we just compute the appoximating polygon
>
>     x <- seq(lower, upper, length=100)
>     y <- sin(x)
>
> and draw it
>
>     polygon(x, y, col = "hotpink")
>
> So here a the full solution.
>
>     x <- seq(0,2*pi, length=100)
>     plot(x, sin(x), type="l")
>     lower <- uniroot(function(x) sin(x)-0.5, lower=0, upper=pi/2)$root
>     upper <- uniroot(function(x) sin(x)-0.5, lower=pi/2, upper=pi)$root
>     x <- seq(lower, upper, length=100)
>     y <- sin(x)
>     polygon(x,y,col="hotpink")
>     lower <- uniroot(function(x) sin(x)+0.5, lower=pi, upper=3*pi/2)$root
>     upper <- uniroot(function(x) sin(x)+0.5, lower=3*pi/2,
upper=2*pi)$root
>     x <- seq(lower, upper, length = 100)
>     y <- sin(x)
>     polygon(x, y, col = "limegreen")
>
> Ross

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list