虽然这是旧的,但我认为以下内容可能会有用。
假设您有一个包含10,000个数据点的数据集,并且您相信它们属于某个分布,并且您想在实际数据的直方图上绘制理想分布的概率密度线。
noise <- 2
noisylognorm <- rlnorm(10000,
mean = 0.25,
sd = 1) +
(noise * runif(10000) - noise / 10)
fitlnorm <- fitdist(subset(noisylognorm,
noisylognorm > 0),
"lnorm")
fitlnorm_density <- density(rlnorm(10000,
mean = fitlnorm$estimate[1],
sd = fitlnorm$estimate[2]))
hist(subset(noisylognorm,
noisylognorm < 25),
breaks = seq(-1, 25, 0.5),
col = "lightblue",
xlim = c(0, 25),
xlab = "value",
ylab = "frequency",
main = paste0("Log Normal Distribution\n",
"noise = ", noise))
lines(fitlnorm_density$x,
10000 * fitlnorm_density$y * 0.5,
type="l",
col = "red")
请注意lines函数中的* 0.5。据我所知,这是必要的,以考虑hist()条形图的宽度。
plot(..., type = "h")
这样的东西。也就是说,这是一种具有垂直线而非条形的直方图。根据您的问题,我推测您想要一个在 y 轴上表示计数的密度图。 - Richie Cotton