I wrote this a bit ago, its far from perfect, but might give you ideas/serve your purpose.
HTH, Josh plot.dist <- function(alpha, from = -5, to = 5, n = 1000, filename = NULL, alternative = c("two.tailed", "greater", "lesser"), distribution = c("normal", "t", "F", "chisq", "binomial"), colour = "black", fill = "skyblue2", ...) { alternative <- match.arg(alternative) ## Calculate alpha level given hypothesis alt.alpha <- switch(alternative, two.tailed = alpha/2, greater = alpha, lesser = alpha) ## use a ’switch’ to pick the right functions based on distribution my.den <- switch(distribution, normal = dnorm, t = dt, F = df, chisq = dchisq, binomial = dbinom) my.dist <- switch(distribution, normal = qnorm, t = qt, F = qf, chisq = qchisq, binomial = qbinom) ## Additional arguments passed via ’...’ e.g., degrees of freedom crit.lower <- my.dist(p = alt.alpha, lower.tail = TRUE, ...) crit.upper <- my.dist(p = alt.alpha, lower.tail = FALSE, ...) ## Calculate alpha (lower) region coordinates cord.x1 <- c(from, seq(from = from, to = crit.lower, length.out = 100), crit.lower) cord.y1 <- c(0, my.den(x = seq(from = from, to = crit.lower, length.out = 100), ...), 0) ## Calculate alpha (upper) region coordinates cord.x2 <- c(crit.upper, seq(from = crit.upper, to = to, length.out = 100), to) cord.y2 <- c(0, my.den(x = seq(from = crit.upper, to = to, length.out = 100), ...), 0) ## Logic test to choose which graphic device to open if(is.null(filename)) { dev.new() } else { pdf(file = filename) } ## plot distribution curve(my.den(x, ...), from = from, to = to, n = n, col = colour, lty = 1, lwd = 2, ylab = "Density", xlab = "Values") ## Add alpha region(s) based on given hypothesis if(!identical(alternative, "greater")) { polygon(x = cord.x1, y = cord.y1, col = fill) } if(!identical(alternative, "lesser")) { polygon(x = cord.x2, y = cord.y2, col = fill) } ## If the PDF device was started, shut it down if(!is.null(filename)) {dev.off()} } On Mon, Nov 8, 2010 at 8:18 AM, Wu Gong <w...@mtmail.mtsu.edu> wrote: > > I want to create a graph to express the idea of the area under a pdf curve, > like > > http://r.789695.n4.nabble.com/file/n3032194/w7295e04.jpg > > Thank you for any help. > > ----- > A R learner. > -- > View this message in context: > http://r.789695.n4.nabble.com/How-to-plot-a-normal-distribution-curve-and-a-shaded-tail-with-alpha-tp3032194p3032194.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. > -- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/ ______________________________________________ R-help@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.