Import data.
str(airquality)
## 'data.frame': 153 obs. of 6 variables:
## $ Ozone : int 41 36 12 18 NA 28 23 19 8 NA ...
## $ Solar.R: int 190 118 149 313 NA NA 299 99 19 194 ...
## $ Wind : num 7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...
## $ Temp : int 67 72 74 62 56 66 65 59 61 69 ...
## $ Month : int 5 5 5 5 5 5 5 5 5 5 ...
## $ Day : int 1 2 3 4 5 6 7 8 9 10 ...
data = as.data.frame(na.omit(airquality))
names(data)
## [1] "Ozone" "Solar.R" "Wind" "Temp" "Month" "Day"
Produce a kernel density plot.
plot(density(data$Ozone), xlab="Ozone (ppb)", ylab="Probability", main="")
Produce a kernel density plot with color.
plot(density(data$Ozone), xlab="Ozone (ppb)", ylab="Probability", main="")
polygon(density(data$Ozone), col="red", border="blue")
rug(data$mpg, col="brown")
Produce a histogram with kernel density plot.
hist(data$Ozone, breaks = 15, freq = F, ylim = c(0, 0.025),
xlab="Ozone (ppb)", ylab="Probability", main="")
lines(density(data$Ozone))
Produce a histogram with normal density curve.
hist(data$Ozone, breaks = 15, freq = F, ylim = c(0, 0.025),
xlab="Ozone (ppb)", ylab="Probability", main="")
curve(dnorm(x, mean=mean(data$Ozone), sd=sd(data$Ozone)), add=T)
Produce a histogram with Gamma density curve.
hist(data$Ozone, breaks = 15, freq = F, ylim = c(0, 0.025),
xlab="Ozone (ppb)", ylab="Probability", main="")
curve(dgamma(x, shape=mean(data$Ozone)^2/var(data$Ozone), scale=var(data$Ozone)/mean(data$Ozone)), add = T)
Produce a comparative kernel density plot with multiple factors.
library(sm)
# Add extra space to right of plot area
par(mar=c(5.1, 4.1, 4.1, 8.1), xpd=TRUE)
month.f = factor(data$Month, levels=c(5,6,7,8,9),
labels=c("Month 5","Month 6","Month 7","Month 8","Month 9"))
sm.density.compare(data$Ozone, data$Month, xlab="Ozone (ppb)", ylab="Probability")
colfill<-c(2:(2+length(levels(month.f))))
legend("right", inset=c(-0.3,0), fill=colfill,
legend=c("Month 5","Month 6","Month 7","Month 8","Month 9"))