User Tools

Site Tools


en:customized_functions:hist.dnorm

hist.dnorm

Author: Tim from CrossValidated, with modification from D. Zeleny

Quick and dirty function which draws histogram overlaid with normal distribution curve.

hist.dnorm.r
hist.dnorm <- function (x, lines.col = 'red', lines.lwd = 2, ...)
{
  hist (x, freq = FALSE, ...)
  lines (seq (min (x), max (x), length = 100), dnorm (seq (min (x), max (x), length = 100), mean = mean (x), sd = sd (x)), col = lines.col, lwd = lines.lwd)  
}

To modify color and thickness of the normal distribution curve, use arguments lines.col and lines.lwd. The function accepts most other arguments for hist, with some limitations.

Example of use (using environmental data from the dataset Vegetation of Carpathian wetlands):

source ('http://www.davidzeleny.net/anadat-r/doku.php/en:customized_functions:hist.dnorm?do=export_code&codeblock=0') # reads the function definition from above
chem <- read.delim ('https://raw.githubusercontent.com/zdealveindy/anadat-r/master/data/chemistry.txt', row.names = 1) # reads the ''chem'' dataset
hist.dnorm (chem$pH, xlab = 'pH', main = 'Histogram of pH')

en/customized_functions/hist.dnorm.txt · Last modified: 2018/03/30 23:04 (external edit)