Bertolt Meyer
2008-Aug-28 14:02 UTC
[R] Help with shading a polygon below a segment of a curve (normal distribution)
Dear R users, I still feel new to R so please apologize if I am doing something stupid here. My use of the polygon() function produces a result that I cannot comprehend: In a plot, I would like to shade the area below a normal distribution. However, I do not want the entire area to be shaded, but just the area on the right side of a vertical line that I draw through the distribution (in order to illustrate the function of a t-test). Here is what I do: scale <- 0.1 x <- seq(-4, 6, scale) y <- dnorm(x) plot(x, y, type = "l", main="t-Test, t = 2.2") linepos <- 2.2 abline(v = linepos) # I try to fill a polygon right of the vertical line: # max(x) - linepos (in this case, 2.2) / scale (0.1) # results in the last 38 elements of x and y. # so I take the last 38 elements of x and y and try to # draw a polygon underneath: cutpoint <- (max(x) - linepos) / scale xt <- x[(length(x)-cutpoint):length(x)] yt <- y[(length(y)-cutpoint):length(y)] # draw the polygon polygon(xt, yt, density = 10 ) As you can see in the result, this is not what I want; some area above the line gets shaded, but not below. Can someone tell me what I am missing? Thank you very much, Bertolt -- Bertolt Meyer Oberassistent Sozialpsychologie, Psychologisches Institut der Universit?t Z?rich Binzm?hlestr. 14, Box 15 CH-8050 Z?rich bmeyer at sozpsy.uzh.ch tel: +41446357282 fax: +41446357279 mob: +41788966111
Peter Dalgaard
2008-Aug-28 14:17 UTC
[R] Help with shading a polygon below a segment of a curve (normal distribution)
Bertolt Meyer wrote:> Dear R users, > > I still feel new to R so please apologize if I am doing something > stupid here. My use of the polygon() function produces a result that I > cannot comprehend: In a plot, I would like to shade the area below a > normal distribution. However, I do not want the entire area to be > shaded, but just the area on the right side of a vertical line that I > draw through the distribution (in order to illustrate the function of > a t-test). Here is what I do: > > scale <- 0.1 > x <- seq(-4, 6, scale) > y <- dnorm(x) > plot(x, y, type = "l", main="t-Test, t = 2.2") > > linepos <- 2.2 > abline(v = linepos) > > # I try to fill a polygon right of the vertical line: > # max(x) - linepos (in this case, 2.2) / scale (0.1) > # results in the last 38 elements of x and y. > # so I take the last 38 elements of x and y and try to > # draw a polygon underneath: > > cutpoint <- (max(x) - linepos) / scale > > xt <- x[(length(x)-cutpoint):length(x)] > yt <- y[(length(y)-cutpoint):length(y)] > > # draw the polygon > > polygon(xt, yt, density = 10 ) > > As you can see in the result, this is not what I want; some area above > the line gets shaded, but not below. Can someone tell me what I am > missing? > > Thank you very much, > Bertolt >Your polygon contains no points on the x axis (yt==0). Try adding this: n <- length(xt) xt <- c(xt[1], xt, xt[n]) yt <- c(0,yt,0) polygon(xt, yt, col="red" ) -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Richard M. Heiberger
2008-Aug-28 14:20 UTC
[R] Help with shading a polygon below a segment of a curve (normaldistribution)
library(HH) normal.and.t.dist() There are many related examples in example(normal.and.t.dist)
Derek Ogle
2008-Aug-28 14:25 UTC
[R] Help with shading a polygon below a segment of a curve (normaldistribution)
Bertolt, The points you send to polygon() do not fully enclose the area you desire. Try adding one more point as such xt <- c(x[(length(x)-cutpoint):length(x)],linepos) yt <- c(y[(length(y)-cutpoint):length(y)],0) polygon(xt, yt, density = 10 ) -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Bertolt Meyer Sent: Thursday, August 28, 2008 9:03 AM To: r-help at r-project.org Subject: [R] Help with shading a polygon below a segment of a curve (normaldistribution) Dear R users, I still feel new to R so please apologize if I am doing something stupid here. My use of the polygon() function produces a result that I cannot comprehend: In a plot, I would like to shade the area below a normal distribution. However, I do not want the entire area to be shaded, but just the area on the right side of a vertical line that I draw through the distribution (in order to illustrate the function of a t-test). Here is what I do: scale <- 0.1 x <- seq(-4, 6, scale) y <- dnorm(x) plot(x, y, type = "l", main="t-Test, t = 2.2") linepos <- 2.2 abline(v = linepos) # I try to fill a polygon right of the vertical line: # max(x) - linepos (in this case, 2.2) / scale (0.1) # results in the last 38 elements of x and y. # so I take the last 38 elements of x and y and try to # draw a polygon underneath: cutpoint <- (max(x) - linepos) / scale xt <- x[(length(x)-cutpoint):length(x)] yt <- y[(length(y)-cutpoint):length(y)] # draw the polygon polygon(xt, yt, density = 10 ) As you can see in the result, this is not what I want; some area above the line gets shaded, but not below. Can someone tell me what I am missing? Thank you very much, Bertolt -- Bertolt Meyer Oberassistent Sozialpsychologie, Psychologisches Institut der Universit?t Z?rich Binzm?hlestr. 14, Box 15 CH-8050 Z?rich bmeyer at sozpsy.uzh.ch tel: +41446357282 fax: +41446357279 mob: +41788966111 ______________________________________________ R-help at r-project.org mailing list 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.
Greg Snow
2008-Aug-28 15:16 UTC
[R] Help with shading a polygon below a segment of a curve (normal distribution)
The power.examp function in the TeachingDemos package (among others) may already do what you want. Even if it does not, it does shade the area under a curve, you can look at the source for the function as an example of where to start. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Bertolt Meyer > Sent: Thursday, August 28, 2008 8:03 AM > To: r-help at r-project.org > Subject: [R] Help with shading a polygon below a segment of a > curve (normal distribution) > > Dear R users, > > I still feel new to R so please apologize if I am doing > something stupid here. My use of the polygon() function > produces a result that I cannot > comprehend: In a plot, I would like to shade the area below a > normal distribution. However, I do not want the entire area > to be shaded, but just the area on the right side of a > vertical line that I draw through the distribution (in order > to illustrate the function of a t-test). Here is what I do: > > scale <- 0.1 > x <- seq(-4, 6, scale) > y <- dnorm(x) > plot(x, y, type = "l", main="t-Test, t = 2.2") > > linepos <- 2.2 > abline(v = linepos) > > # I try to fill a polygon right of the vertical line: > # max(x) - linepos (in this case, 2.2) / scale (0.1) # > results in the last 38 elements of x and y. > # so I take the last 38 elements of x and y and try to # draw > a polygon underneath: > > cutpoint <- (max(x) - linepos) / scale > > xt <- x[(length(x)-cutpoint):length(x)] > yt <- y[(length(y)-cutpoint):length(y)] > > # draw the polygon > > polygon(xt, yt, density = 10 ) > > As you can see in the result, this is not what I want; some > area above the line gets shaded, but not below. Can someone > tell me what I am missing? > > Thank you very much, > Bertolt > > -- > Bertolt Meyer > Oberassistent > Sozialpsychologie, Psychologisches Institut der Universit?t > Z?rich Binzm?hlestr. 14, Box 15 CH-8050 Z?rich > > bmeyer at sozpsy.uzh.ch > tel: +41446357282 > fax: +41446357279 > mob: +41788966111 > > ______________________________________________ > R-help at r-project.org mailing list > 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. >
Maybe Matching Threads
- Newbie question: How to use tapply() on several vectors simultaneously
- vector indexing problem in multilevel data: assigning a specific value to all group members
- marginality principle / selecting the right type of SS for an interaction hypothesis
- lmer() causes segfault
- Multilevel model with lme(): Weird degrees of freedom (group level df > # of groups)