是否有R中的功能适合的曲线以直方图?

让我们假设你有以下直方图

hist(c(rep(65, times=5), rep(25, times=5), rep(35, times=10), rep(45, times=4)))

它看起来正常,但它的歪斜。我想适合正常曲线偏斜环绕该直方图。

这个问题是相当基本的,但我似乎无法找到在互联网上R上的答案。

有帮助吗?

解决方案

如果我正确地理解你的问题,那么你可能想用柱状图沿着密度估计:

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 counts
lines(density(X))             # add a density estimate with defaults
lines(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 counts
lines(density(X), col="blue", lwd=2) # add a density estimate with defaults
lines(density(X, adjust=2), lty="dotted", col="darkgreen", lwd=2) 

用它产生的图表沿着:

“在这里输入的图像描述”

其他提示

这样的东西很容易与GGPLOT2

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()

下面是我做的方式:

foo <- rnorm(100, mean=1, sd=2)
hist(foo, prob=TRUE)
curve(dnorm(x, mean=mean(foo), sd=sd(foo)), add=TRUE)

一个奖金锻炼与GGPLOT2包来做到这一点...

我有同样的问题,但德克的解决方案似乎并没有工作。 每次我得到这样的警告messege

"prob" is not a graphical parameter

我通过?hist阅读并发现约freq: a logical vector set TRUE by default.

这为我工作的代码是

hist(x,freq=FALSE)
lines(density(x),na.rm=TRUE)
scroll top