Density Plots |
R has several functions for generating smoothed histograms, or density plots. We will focus on the "density" function.
Basic usage is starightforward:
plot(density(Glucose1[,1]))
# or fancier:
plot(density(Glucose1[,1]), col="red", lwd=2, lty=3,
main="Glucose Test 1 Density")
abline(h=0)
rug(Glucose1[,1])
The density function produces
a data structure with several components - the first ($x) is the set of
points on the x-axis where the density was evaluated; the second ($y)
is the height of the density curve at those points.
d <- density(Glucose1[,1])
names(d)
The main issue with density estimates is how much smoothing to do.
There is a "bandwidth" parameter (bw)
in the density function that determines this.
Try a few different values:
plot(density(Glucose1[,1],bw=.5),col="blue",lwd=2)
If the bandwidth parameter is too small, you get a punk hair style:
plot(density(Glucose1[,1],bw=100),col="purple",lwd=2)
If the width parameter is too large, you get a nice smooth curve that looks
the same no matter what your data were!
hist(Glucose1[,1],freq=F,col="tan")
lines(density(Glucose1[,1]),lwd=2,col="red")
You can experiment with different line styles:
hist(Glucose1[,1],freq=F,col="salmon")
lines(density(Glucose1[,1]),lty=2,lwd=2,col="blue")
Try the digits 0 through 9 for the lty parameter.
Here is a "dotplot" added to a density curve by the "points" function; the original data values are plotted on the x axis below the density cureve:
plot(density(Glucose1[,1]))
points(Glucose1[,1],rep(0,53))
The rug() function does something similar, but nicer.
plot(density(Glucose1[,1]),col="purple",lwd=2,
main="Glucose test 1 density")
rug(Glucose1[,1],lwd=2,col="tomato")
Try both at once!
Albyn Jones