# Polygon Classic List Threaded 10 messages Open this post in threaded view
|

## Polygon

 Can someone help me with polygon. The following codes are self-runnable and mark a shaded area under the standard normal curve in the x-range (-3,-1). Is there a way to also mark the area in (1,3), at the same time. That is, I want shaded areas in both tails. Thank you... === # Create data for the area to shade cord.x <- c(-3,seq(-3,-1,0.01),-1) cord.y <- c(0,dnorm(seq(-3,-1,0.01)),0) # Make a curve curve(dnorm(x,0,1), xlim=c(-3,3), main='Standard Normal') # Add the shaded area. polygon(cord.x,cord.y,col='skyblue') -- [hidden email] (S.T. Yen)         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Polygon

 Hello, Continue with polygon(-rev(cord.x), rev(cord.y), col = 'skyblue') Hope this helps, Rui Barradas Às 17:25 de 02/10/2018, Steven Yen escreveu: > Can someone help me with polygon. The following codes are self-runnable > and mark a shaded area under the standard normal curve in the x-range > (-3,-1). > Is there a way to also mark the area in (1,3), at the same time. > That is, I want shaded areas in both tails. Thank you... > > === > # Create data for the area to shade > cord.x <- c(-3,seq(-3,-1,0.01),-1) > cord.y <- c(0,dnorm(seq(-3,-1,0.01)),0) > > # Make a curve > curve(dnorm(x,0,1), xlim=c(-3,3), main='Standard Normal') > > # Add the shaded area. > polygon(cord.x,cord.y,col='skyblue') > ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Polygon

 Great. Thanks! It did wonders. 1. Is there a way to suppress the obvious tick stops (-3,-2,-1,0,1,2,3) and mark only the ticks -1.96 and 1.96. 2. Better yet, to draw vertical lines at x = -1.96 and x = 1.96. Thanks. Steven On 10/3/2018 12:51 AM, Rui Barradas wrote: > Hello, > > Continue with > > > polygon(-rev(cord.x), rev(cord.y), col = 'skyblue') > > > Hope this helps, > > Rui Barradas > > Às 17:25 de 02/10/2018, Steven Yen escreveu: >> Can someone help me with polygon. The following codes are self-runnable >> and mark a shaded area under the standard normal curve in the x-range >> (-3,-1). >> Is there a way to also mark the area in (1,3), at the same time. >> That is, I want shaded areas in both tails. Thank you... >> >> === >> # Create data for the area to shade >> cord.x <- c(-3,seq(-3,-1,0.01),-1) >> cord.y <- c(0,dnorm(seq(-3,-1,0.01)),0) >> >> # Make a curve >> curve(dnorm(x,0,1), xlim=c(-3,3), main='Standard Normal') >> >> # Add the shaded area. >> polygon(cord.x,cord.y,col='skyblue') >> > -- [hidden email] (S.T. Yen)         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Polygon

Open this post in threaded view
|

## Re: Polygon

Open this post in threaded view
|

## Re: Polygon

Open this post in threaded view
|

## Re: Polygon

 In reply to this post by David Winsemius 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)) -- [hidden email] (S.T. Yen)         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Polygon

 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:[hidden email]] On Behalf Of Steven Yen Sent: Sunday, October 21, 2018 1:47 PM To: David Winsemius <[hidden email]> Cc: [hidden email] 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)) -- [hidden email] (S.T. Yen)         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
 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 <[hidden email]> Sent: Monday, October 22, 2018 11:53 AM To: David L Carlson <[hidden email]> 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)  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)))  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))  -0.8185917 abs(polyarea(c(-1, cord.x, 2), c(0, cord.y, 0)))  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:[hidden email] Sent: Sunday, October 21, 2018 9:54 PM To: David L Carlson mailto:[hidden email] 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:[hidden email]] On Behalf Of Steven Yen Sent: Sunday, October 21, 2018 1:47 PM To: David Winsemius mailto:[hidden email] Cc: mailto:[hidden email] 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:[hidden email] (S.T. Yen) ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code. Plot.png (10K) Download Attachment