Histogram & Density Plot
What is a Histogram?
A histogram is a graphical representation of the frequency distribution of cardinally scaled characteristics. It requires the division of the data into bins (classes), which can have a constant or variable width (see image on the right by DanielPenfield) on wikimedia.org. Directly adjacent rectangles of the width of the respective class are drawn, the area of which represents the (relative or absolute) class frequencies. The height of each rectangle then represents the (relative or absolute) frequency density, i.e. the (relative or absolute) frequency divided by the width of the corresponding class.
Built-in Datasets
R comes with several built-in data sets, which are generally used as demo data for playing with R functions. These built-in data sets are ideal for practicing plotting. To see the list of pre-loaded data, type the function data():
- “mtcars”: Motor Trend Car Road Tests
- “iris”: measurements for 50 flowers from each of 3 species of iris
- “ToothGrowth”: effect of vitamin C on tooth growth in Guinea pigs
- “PlantGrowth”: results of an experiment to compare yields
- “USArrests”: statistics about violent crime rates by US state
- and many many more…
Reminder
- Load a built-in R data set:
data(“dataset_name”) - Inspect the data set:
head(dataset_name)
Theory
You can create histograms with hist(x), where x is a numeric vector. With equal-width bins, the default vertical axis shows counts. With freq=FALSE, heights show probability densities: each bar’s area is the proportion of observations in its bin, and the total area is one. Use this scale when bin widths differ. The breaks= argument can specify exact bin boundaries or suggest a number of bins; R may adjust a suggested number to obtain convenient boundaries. See the R documentation for hist().
Simple Histogram with the dataset mtcars. If you want to learn more about mtcars, type this: ?mtcars
# Loading dataset
data(mtcars)
# creating histograms with mpg = US Miles per gallon and hp = horsepower
mpg <- hist(mtcars$mpg)
hp <- hist(mtcars$hp)
Histogram with Breaks (Bins) and Color
hist(mtcars$mpg, breaks=10, col="red")
hist(mtcars$hp, breaks=6, col="blue")
A histogram’s appearance depends on both the widths and the positions of its bin boundaries. Narrow bins show more local detail but may exaggerate random variation; wide bins can hide features. Compare several reasonable choices before interpreting peaks, gaps or skewness. In the example above, breaks=10 is a suggestion, not a guarantee of exactly ten bins.
Kernel Density Plots
A kernel density plot gives a smooth estimate of a numeric variable’s probability density. Create one with plot(density(x)). It is another view of the distribution, whose appearance depends on the smoothing bandwidth: smaller bandwidths give more detail and larger bandwidths more smoothing. Use bw= to set the bandwidth or adjust= to multiply the automatically chosen bandwidth. Neither a histogram nor a density plot is always preferable; check whether the features you describe persist across reasonable bin or bandwidth choices. See the R documentation for density().
d <- density(mtcars$mpg) # returns the density data
plot(d, main="Kernel Density of Miles/Gallon") # Unfilled Density Plot
polygon(d, col="red", border="blue") # Filled Density Plot
The vertical axis shows density, not a count or the probability of an exact value. Probability is represented by area over an interval. The estimated curve can extend beyond the observed minimum and maximum because the default Gaussian kernels spread mass around each observation. To explore bandwidth sensitivity, compare plot(density(mtcars$mpg, adjust=0.5)) with plot(density(mtcars$mpg, adjust=2)).