[R] Polygon
David L Carlson
dc@r|@on @end|ng |rom t@mu@edu
Sun Oct 21 22:24:24 CEST 2018
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 <dwinsemius using comcast.net>
Cc: 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))
--
styen using ntu.edu.tw (S.T. Yen)
[[alternative HTML version deleted]]
______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list