I like R too. Here is a more or less generic function to plot any probability distribution from the base R functions. It should not be difficult to extend the code with functions available in other packages, e.g. SuppDists.
plot.func <- function(distr=c("beta", "binom", "cauchy", "chisq",
"exp", "f","gamma", "geom", "hyper",
"logis", "lnorm", "nbinom", "norm",
"pois", "t", "unif", "weibull"),
what=c("pdf", "cdf"), params=list(), type="b",
xlim=c(0, 1), log=FALSE, n=101, add=FALSE, ...) {
what <- match.arg(what)
d <- match.fun(paste(switch(what, pdf = "d", cdf = "p"),
distr, sep=""))
# Define x-values (because we won't use 'curve') as last parameter
# (with pdf, it should be 'x', while for cdf it is 'q').
len <- length(params)
params[[len+1]] <- seq(xlim[1], xlim[2], length=n)
if (add) lines(params[[len+1]], do.call(d, params), type, ...)
else plot(params[[len+1]], do.call(d, params), type, ...)
}
It's a bit crappy and I haven't tested it a lot. The params list must obey R's conventions for naming {C|P}DF parameters (e.g., shape and scale for the Weibull distribution, and not a or b).
There's room for improvement, especially about the way it handles multiple plotting on the same graphic device (and, actually, passing vector of parameters only works as a side-effect when type="p"). Also, there's not much parameter checking!
Here are some examples of use:
# Normal CDF
xl <- c(-5, 5)
plot.func("norm", what="pdf", params=list(mean=1, sd=1.2),
xlim=xl, ylim=c(0,.5), cex=.8, type="l", xlab="x", ylab="F(x)")
plot.func("norm", what="pdf", params=list(mean=3, sd=.8),
xlim=xl, add=TRUE, pch=19, cex=.8)
plot.func("norm", what="pdf", params=list(mean=.5, sd=1.3), n=201,
xlim=xl, add=TRUE, pch=19, cex=.4, type="p", col="steelblue")
title(main="Some gaussian PDFs")
# Standard normal PDF
plot.func("norm", "cdf", xlab="Quantile (x)", ylab="P(X<x)", xlim=c(-3,3), type="l",
main="Some gaussian CDFs")
plot.func("norm", "cdf", list(sd=c(0.5,1.5)), xlim=c(-3,3), add=TRUE,
type="p", pch=c("o","+"), n=201, cex=.8)
legend("topleft", paste("N(0;", c(1,0.5,1.5), ")", sep=""),
lty=c(1,NA,NA), pch=c(NA,"o","+"), bty="n")
# Weibull distribution
s <- c(.5,.75,1)
plot.func("weibull", what="pdf", xlim=c(0,1), params=list(shape=s),
col=1:3, type="p", n=301, pch=19, cex=.6, xlab="", ylab="")
title(main="Weibull distribution", xlab="x", ylab="F(x)")
legend("topright", legend=as.character(s), title="Shape", col=1:3, pch=19)
