[R] Help with shading a polygon below a segment of a curve (normal distribution)

Bertolt Meyer bmeyer at sozpsy.uzh.ch
Thu Aug 28 16:02:32 CEST 2008


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



More information about the R-help mailing list