# 基础作图法绘制直方图与密度曲线的结合
par(mar = c(1.8, 3, 0.5, 0.1), mgp = c(2, 0.5, 0), mfrow = c(1, 2))
data(geyser, package = "MASS")

hist(geyser$waiting, freq = FALSE, main = "")
lines(density(geyser$waiting))

hst = hist(geyser$waiting, probability = TRUE,
           main = "", xlab = "waiting")
d = density(geyser$waiting)
polygon(c(min(d$x), d$x, max(d$x)), c(0, d$y, 0),
        col = "lightgray", border = NA)
lines(d)
ht = NULL
brk = seq(40, 110, 5)
for (i in brk) ht = c(ht, d$y[which.min(abs(d$x - i))])
segments(brk, 0, brk, ht, lty = 3)
