Skip to content

Commit

Permalink
Adjust normTail function so it is possible to add a normal distributi…
Browse files Browse the repository at this point in the history
…on to an existing plot
  • Loading branch information
OpenIntroOrg committed Jun 22, 2015
1 parent 9b3f5c3 commit 7cfb2ed
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -15,3 +15,4 @@ main.toc
*gitignore~
*.Rapp.history
archive/*
*.DS_Store
31 changes: 18 additions & 13 deletions openintro/R/normTail.R
Expand Up @@ -17,6 +17,7 @@
xLab = c('number', 'symbol'),
cex.axis = 1,
xAxisIncr = 1,
add = FALSE,
...) {
if (is.null(xlim)[1]) {
xlim <- m + c(-1, 1) * 3.5 * s
Expand All @@ -29,15 +30,19 @@
if (is.null(ylim)[1]) {
ylim <- range(c(0, y))
}
plot(x, y,
type = 'l',
xlim = xlim,
ylim = ylim,
xlab = xlab,
ylab = ylab,
axes = FALSE,
col = curveColor,
...)
if (add) {
lines(x, y, col = curveColor, ...)
} else {
plot(x, y,
type = 'l',
xlim = xlim,
ylim = ylim,
xlab = xlab,
ylab = ylab,
axes = FALSE,
col = curveColor,
...)
}
if (!is.null(L[1])) {
these <- (x <= L)
X <- c(x[these][1], x[these], rev(x[these])[1])
Expand All @@ -57,7 +62,7 @@
polygon(X, Y, border = border, col = col)
}

if (axes == 1 || axes > 2) {
if (!add && (axes == 1 || axes > 2)) {
if (xLab[1] == 'symbol') {
xAt <- m + (-3:3) * s
xLab <- expression(mu - 3 * sigma,
Expand All @@ -77,12 +82,12 @@
xLab <- round(xAt, digits = digits)
}
}
if (axes > 2) {
if (!add && axes > 2) {
axis(1, at = xAt, labels = xLab, cex.axis = cex.axis)
buildAxis(2, c(y, 0), n = 3, nMax = 3, cex.axis = cex.axis)
} else if (axes > 1) {
} else if (!add && axes > 1) {
buildAxis(2, c(y, 0), n = 3, nMax = 3, cex.axis = cex.axis)
} else if (axes > 0) {
} else if (!add && axes > 0) {
axis(1, at = xAt, labels = xLab, cex.axis = cex.axis)
}
abline(h = 0)
Expand Down
71 changes: 45 additions & 26 deletions openintro/man/normTail.Rd
@@ -1,48 +1,67 @@
\name{normTail}
\alias{normTail}
\title{ Normal distribution tails }
\description{ Produce a normal (or t) distribution and shaded tail. }
\title{Normal distribution tails}
\description{Produce a normal (or t) distribution and shaded tail.}
\usage{
normTail(m = 0, s = 1, L = NULL, U = NULL, M = NULL, df=1000,
curveColor=1, border = 1, col = "#CCCCCC", xlim = NULL,
ylim=NULL, xlab = "", ylab = "", digits = 2, axes = 1,
detail = 999, xLab = c("number", "symbol"), cex.axis = 1,
xAxisIncr=1, ...)
xAxisIncr = 1, add = FALSE, ...)
}
\arguments{
\item{m}{ Numerical value for the distribution mean. }
\item{s}{ Numerical value for the distribution standard deviation. }
\item{L}{ Numerical value representing the cutoff for a shaded lower tail. }
\item{U}{ Numerical value representing the cutoff for a shaded upper tail. }
\item{M}{ Numerical value representing the cutoff for a shaded central region. }
\item{df}{ Numerical value describing the degrees of freedom. Default is \code{1000}, which results in a nearly normal distribution. Small values may be useful to emphasize small tails. }
\item{L}{ Numerical value representing the cutoff for a shaded
lower tail. }
\item{U}{ Numerical value representing the cutoff for a shaded
upper tail. }
\item{M}{ Numerical value representing the cutoff for a shaded
central region. }
\item{df}{ Numerical value describing the degrees of freedom.
Default is \code{1000}, which results in a nearly normal
distribution. Small values may be useful to emphasize
small tails. }
\item{curveColor}{ The color for the distribution curve. }
\item{border}{ The color for the border of the shaded area. }
\item{col}{ The color for filling the shaded area. }
\item{xlim}{ Limits for the x axis. }
\item{ylim}{ Limits for the y axis. }
\item{xlab}{ A title for the x axis. }
\item{ylab}{ A title for the y axis. }
\item{digits}{ The maximum number of digits past the decimal to use in axes values. }
\item{axes}{ A numeric value denoting whether to draw both axes (\code{3}), only the vertical axes (\code{2}), only the horizontal axes (\code{1}, the default), or no axes (\code{0}). }
\item{detail}{ A number describing the number of points to use in drawing the normal curve. Smaller values correspond to a less smooth curve but reduced memory usage in the final file. }
\item{xLab}{ If \code{"number"}, then the axis is drawn at the mean, and every standard deviation out until the third standard deviation. If \code{"symbol"}, then Greek letters are used for standard deviations from three standard deviations from the mean. }
\item{cex.axis}{ Numerical value controlling the size of the axis labels. }
\item{xAxisIncr}{ A number describing how often axis labels are placed, scaled by standard deviations. This argument is ignored if \code{xLab="symbol"}. }
\item{digits}{ The maximum number of digits past the decimal
to use in axes values. }
\item{axes}{ A numeric value denoting whether to draw both axes
(\code{3}), only the vertical axes (\code{2}), only the
horizontal axes (\code{1}, the default), or no axes
(\code{0}). }
\item{detail}{ A number describing the number of points to
use in drawing the normal curve. Smaller values correspond
to a less smooth curve but reduced memory usage in the
final file. }
\item{xLab}{ If \code{"number"}, then the axis is drawn at
the mean, and every standard deviation out until the third
standard deviation. If \code{"symbol"}, then Greek letters
are used for standard deviations from three standard
deviations from the mean. }
\item{cex.axis}{ Numerical value controlling the size of the
axis labels. }
\item{xAxisIncr}{ A number describing how often axis labels are
placed, scaled by standard deviations. This argument is ignored
if \code{xLab = "symbol"}. }
\item{add}{Boolean indicating whether to add this normal curve to
the existing plot.}
\item{\dots}{ Additional arguments to \code{plot}. }
}
\author{ DM Diez }
\seealso{
\code{\link{buildAxis}}
}
\author{David Diez}
\seealso{\code{\link{buildAxis}}}
\examples{
par(mfrow=c(2,3), mar=c(3,3,1,1))
normTail(3,2,5)
normTail(3,2,1, xLab='symbol')
normTail(3,2,M=1:2, xLab='symbol', cex.axis=0.8)
normTail(3,2,U=5,axes=FALSE)
normTail(L=-1, U=2, M=c(0,1), axes=3, xAxisIncr=2)
normTail(L=-1, U=2, M=c(0,1), xLab='symbol', cex.axis=0.8, xAxisIncr=2)
par(mfrow = c(2, 3), mar = c(3, 3, 1, 1))
normTail(3, 2, 5)
normTail(3, 2, 1, xLab = 'symbol')
normTail(3, 2, M = 1:2, xLab = 'symbol', cex.axis = 0.8)
normTail(3, 2, U = 5, axes = FALSE)
normTail(L = -1, U = 2, M = c(0, 1), axes = 3, xAxisIncr = 2)
normTail(L = -1, U = 2, M = c(0, 1),
xLab = 'symbol', cex.axis = 0.8, xAxisIncr = 2)
}
\keyword{ normal }
\keyword{ tail }

0 comments on commit 7cfb2ed

Please sign in to comment.