[R] Polygon

David L Carlson dc@r|@on @end|ng |rom t@mu@edu
Mon Oct 22 15:34:23 CEST 2018

Yes, par(oldp) resets the parameter to its original value.

The simplest way to get the area in this case would be to use pnorm():

> pnorm(2) - pnorm(-1)
[1] 0.8185946

If you have an arbitrary polygon, you can use polyarea() in the pracma package:

> library(pracma)
> polyarea(rev(c(-1, cord.x, 2)), rev(c(0, cord.y, 0)))
[1] 0.8185917

The second differs slightly from the first because it is an approximation based on 101 points (the default for the curve() function). The polyarea() function wants the polygon to be defined counter-clockwise so we use rev() to reverse the coordinates. Without this you will get a negative area, but you can always use abs() to fix that:

> polyarea(c(-1, cord.x, 2), c(0, cord.y, 0))
[1] -0.8185917
> abs(polyarea(c(-1, cord.x, 2), c(0, cord.y, 0)))
[1] 0.8185917

One more point. Don’t use abbreviations for TRUE and FALSE (e.g. frame=F). FALSE cannot be redefined, but F can be. If you used a variable called F in earlier code, it could change the behavior of curve():

> curve(dnorm(x,0,1), xlim=c(-5, 5), frame=F)
> FALSE <- 1
Error in FALSE <- 1 : invalid (do_set) left-hand side to assignment
> F <- 1
> curve(dnorm(x,0,1), xlim=c(-5, 5), frame=F)

David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77843-4352

From: Steven Yen <styen using ntu.edu.tw> 
Sent: Sunday, October 21, 2018 9:54 PM
To: David L Carlson <dcarlson using tamu.edu>
Subject: Re: [R] Polygon

Thank you David. This is great!!
I can follow all you do, except one. What does the last statement do: par(oldp). I would think that it reset the parameters, needed only if there is a next job.

I am a little embarrassed to push this further, but let me see if I can bounce another thing out of this.

Is it possible to have a "call-out", somehow indicating that the probability associated witht he shaded area is, say, 0.45. Is this possible? Am I asking too much of R? Fine if not. Thanks again!!!
On 10/22/2018 4:24 AM, David L Carlson wrote:
I think this does what you want.

z1<- -1
z2<-  2
oldp <- par(mgp=c(3, .5, 0))
curve(dnorm(x,0,1), xlim=c(-5, 5),main="Standard Normal",
    xaxt="n", frame=F, xlab="")
cord.x<- jj
cord.y<- dnorm(jj)
polygon(c(-1, cord.x, 2), c(0, cord.y, 0), col="skyblue")
axis(1, at=c(-5, z1, 0, z2, 5), lab=(c(-5,z1,0,z2,5) + 5)*2, line=2)

When you assign a vector to an object, you do not need to use c(), e.g. cord.x <- jj. I expanded xlim= since you were labeling the axis at -5, and 5, but only drawing the plot from -4 to 4.

To get the polygon to work, you need to add points to the baseline at at either end.

The line= argument in axis() lets you put an axis line into the margin of the plot. I used the mgp= argument in par() to close the space between the tickmarks and their values.
David L. Carlson
Department of Anthropology
Texas A&M University

-----Original Message-----
From: R-help [mailto:r-help-bounces using r-project.org] On Behalf Of Steven Yen
Sent: Sunday, October 21, 2018 1:47 PM
To: David Winsemius mailto:dwinsemius using comcast.net
Cc: mailto:r-help using r-project.org
Subject: Re: [R] Polygon

David, Rui, and All:
1. I need a helping hand with the polygon statement below so that I can 
have the area under the curve highlighted, between (z1,z2).
2. Is it possible to label the X-axis with in two scale, in the current 
z-scale and another, say x = (z+5)*2?
Thank you.

z1<- -1
z2<-  2

mailto:styen using ntu.edu.tw (S.T. Yen)

More information about the R-help mailing list