Learn R Programming

lessR (version 1.5.2)

color.density: Data Based Density Curves with Color and Histogram

Description

Plots a normal density curve and/or a general density curve superimposed over a histogram, all estimated from the data.

Usage

color.density(x, col.nrm="darkblue", col.gen="blue", col.bg="snow1",  
         col.grid="grey90", col.hist="grey86",
         col.fill.nrm=rgb(80,150,200, alpha=70, max=255), 
         col.fill.gen=rgb(250,210,230, alpha=70, max=255),
         type=c("both", "general", "normal"),
         xlab=NULL, main=NULL, ...)

Arguments

x
Variable for which to construct the histogram.
col.nrm
Color of the normal curve.
col.gen
Color of the general density curve.
col.bg
Color of the plot background.
col.grid
Color of the grid lines.
col.hist
Default is to display the histogram in a light gray. To suppress, the histogram, specify a color of "transparent".
col.fill.nrm
Fill color for the estimated normal curve, with a transparent blue as the default.
col.fill.gen
Fill color for the estimated general density curve, with a transparent light red as the default.
type
Type of density curve plotted. By default, both the general density and the normal density are plotted.
xlab
Label for x-axis.
main
Title of graph.
...
Other parameter values for graphics as defined processed by plot, including xlim, ylim, lwd and cex.lab, col.main, density,

Details

Results are based on the standard dnorm function and density R functions for estimating densities from data, as well as the hist function for calculating a histogram. Colors are provided by default and can also be specified.

By default, the histogram is displayed in a light gray, as a background for the normal and/or general estimated density curves, though this color can be changed. Using the alpha option for the rgb function, the density curves are by default plotted with transparent colors to facilitate comparison to the background histogram.

No user-supplied values are passed to the hist function itself, so only default values are permitted, such as the default bins as calculated by the default for the hist function. Use the color.hist function in this package for control over the parameters of the histogram.

The limits for the axes are automatically calculated so as to provide sufficient space for the density curves and histogram, and should generally not require user intervention. Also, the curves are centered over the plot window so that the resulting density curves are symmetric even if the underlying histogram is not. The estimated normal curve is based on the corresponding sample mean and standard deviation.

See Also

dnorm, density, hist, plot, rgb.

Examples

Run this code
# generate 100 random normal data values
y <- rnorm(100)

# normal curve and general density curves superimposed over histogram
# all defaults
color.density(y)

# suppress the histogram, leaving only the density curves
# specify x-axis label per the xlab option for the plot function
color.density(y, col.hist="transparent", xlab="My Var")

# specify (non-transparent) colors for the curves,
# to make transparent, need alpha option for the rgb function
color.density(y, col.fill.nrm="darkgreen", col.fill.gen="plum")

# display only the general estimated density
#  so do not display the estimated normal curve
# specify the bandwidth for the general density curve,
#  use the standard bw option for the density function
color.density(y, type="general", bw=.6)

Run the code above in your browser using DataLab