将密度曲线拟合成R中的直方图R中是否有与直方图拟合曲线的函数?假设你有下面的直方图hist(c(rep(65, times=5), rep(25, times=5), rep(35, times=10), rep(45, times=4)))它看起来很正常,但它是倾斜的。我想要拟合一个正常的曲线,是倾斜的,围绕这个直方图。这个问题是相当基本的,但我似乎无法在互联网上找到R的答案。
3 回答
一只萌萌小番薯
TA贡献1795条经验 获得超7个赞
X <- c(rep(65, times=5), rep(25, times=5), rep(35, times=10), rep(45, times=4))hist(X, prob=TRUE) # prob=TRUE for probabilities not countslines(density(X)) # add a density estimate with defaultslines(density(X, adjust=2), lty="dotted") # add another "smoother" density
稍后编辑很长时间:
X <- c(rep(65, times=5), rep(25, times=5), rep(35, times=10), rep(45, times=4))hist(X, prob=TRUE, col="grey") # prob=TRUE for probabilities not countslines(density(X), col="blue", lwd=2) # add a density estimate with defaultslines(density(X, adjust=2), lty="dotted", col="darkgreen", lwd=2)
慕后森
TA贡献1802条经验 获得超5个赞
library(ggplot2)dataset <- data.frame(X = c(rep(65, times=5), rep(25, times=5), rep(35, times=10), rep(45, times=4)))ggplot(dataset, aes(x = X)) + geom_histogram(aes(y = ..density..)) + geom_density()
ggplot(dataset, aes(x = X)) + geom_histogram(aes(y = ..density..), binwidth = 5) + geom_density()
慕沐林林
TA贡献2016条经验 获得超9个赞
foo <- rnorm(100, mean=1, sd=2)hist(foo, prob=TRUE)curve(dnorm(x, mean=mean(foo), sd=sd(foo)), add=TRUE)
- 3 回答
- 0 关注
- 1120 浏览
添加回答
举报
0/150
提交
取消