Wu Gong
2010-Nov-08 16:18 UTC
[R] How to plot a normal distribution curve and a shaded tail with alpha?
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.
David Winsemius
2010-Nov-08 16:43 UTC
[R] How to plot a normal distribution curve and a shaded tail with alpha?
On Nov 8, 2010, at 11:18 AM, Wu Gong 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.jpgI think you could have titled this request: Yet Another Request to Do My Searching For Me: http://search.r-project.org/cgi-bin/namazu.cgi?query=plot+normal+curve+with+shading&max=100&result=normal&sort=score&idxname=functions&idxname=Rhelp08&idxname=Rhelp10&idxname=Rhelp02 The R "search space" is littered with examples and that search string is not likely to uncover them all, but is rather just one example. -- David> > 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 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.David Winsemius, MD West Hartford, CT
Greg Snow
2010-Nov-08 18:53 UTC
[R] How to plot a normal distribution curve and a shaded tail with alpha?
Look at power.examp in the TeachingDemos package. If that plot is not good enough, you can steal the code and modify to your specifications. -- 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 Wu Gong > Sent: Monday, November 08, 2010 9:18 AM > To: r-help at r-project.org > Subject: [R] How to plot a normal distribution curve and a shaded tail > with alpha? > > > 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 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.
James Long
2010-Nov-08 20:47 UTC
[R] How to plot a normal distribution curve and a shaded tail with alpha?
Here's an example of a PDF with the tails shaded: http://stackoverflow.com/q/3494593/37751 -JD On Mon, Nov 8, 2010 at 10:18 AM, Wu Gong <wg2f at 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 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. >
Joshua Wiley
2010-Nov-08 20:54 UTC
[R] How to plot a normal distribution curve and a shaded tail with alpha?
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 <wg2f at 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 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.
>
--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/
RICHARD M. HEIBERGER
2010-Nov-08 21:21 UTC
[R] How to plot a normal distribution curve and a shaded tail with alpha?
You can use the
normal.and.t.dist()
function in the HH package, available from CRAN with
install.packages("HH")
This works on all platforms. There is menu access to the function through
Rcmdr if you install
install.packages("RcmdrPlugin.HH")
If you are on Windows, you can additionally control that function
interactively
from an Excel spreadsheet with the RExcel package.
Install with the
RthroughExcelWorkbooksInstaller package from CRAN.
Rich
On Mon, Nov 8, 2010 at 11:18 AM, Wu Gong <wg2f@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<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]