Top Menu

Histograms and Density Plots

Histograms

You can create histograms with the function hist(x) where x is a numeric vector of values to be plotted. The option freq=FALSE plots probability densities instead of frequencies. The option breaks= controls the number of bins.

# Simple Histogram
hist(mtcars$mpg)

click to view

# Colored Histogram with Different Number of Bins
hist(mtcars$mpg, breaks=12, col="red")

Comparing Groups VIA Kernal Density

The sm.density.compare( ) function in the smpackage allows you to superimpose the kernal density plots of two or more groups. The format is sm.density.compare(x, factor) where x is a numeric vector and factor is the grouping variable.