[R] Polygon
David L Carlson
dc@r|@on @end|ng |rom t@mu@edu
Mon Oct 22 19:19:37 CEST 2018
Putting it all together:
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="")
jj<-seq(z1,z2,0.01)
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))
abline(v=c(z1,z2))
axis(1, at=c(-5, z1, 0, z2, 5), lab=(c(-5,z1,0,z2,5) + 5)*2, line=2)
segments(.5, .1, 3, .2, lwd=2)
lbl <- paste0("Area = ", round(pnorm(2) - pnorm(-1), 4))
text(3, .2, lbl, pos=4, offset=.25)
par(oldp)
----------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77843-4352
From: Steven Yen <styen using ntu.edu.tw>
Sent: Monday, October 22, 2018 11:53 AM
To: David L Carlson <dcarlson using tamu.edu>
Subject: Re: [R] Polygon
Thanks David. I do know what to do with pnorm.
By call-out I was hoping to get indication/annotation of probability like the attached.
Maybe I am asking too much of the data, as I suspect R is not meant to do that.
I appreciate your help. I am just trying to make these graphs for stat teaching without graphic software like correl draw.
On 10/22/2018 9:34 PM, David L Carlson wrote:
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 mailto:styen using ntu.edu.tw
Sent: Sunday, October 21, 2018 9:54 PM
To: David L Carlson mailto: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="")
jj<-seq(z1,z2,0.01)
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))
abline(v=c(z1,z2))
axis(1, at=c(-5, z1, 0, z2, 5), lab=(c(-5,z1,0,z2,5) + 5)*2, line=2)
par(oldp)
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:
Greetings.
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
curve(dnorm(x,0,1),xlim=c(-4,4),main="Standard
Normal",xaxt="n",frame=F,xlab="z")
jj<-seq(z1,z2,0.01)
cord.x<-c(jj)
cord.y<-c(dnorm(jj))
#polygon(cord.x,cord.y,col="skyblue")
axis(1,at=c(-5,z1,0,z2,5),lab=c(-5,z1,0,z2,5))
abline(v=c(z1,z2))
--
mailto:styen using ntu.edu.tw (S.T. Yen)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Plot.png
Type: image/png
Size: 8028 bytes
Desc: Plot.png
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20181022/89fd1580/attachment-0002.png>
More information about the R-help
mailing list