R将正态曲线与概率直方图重叠

时间:2015-10-12 14:03:16

标签: r histogram

在R中我能够将正常曲线与密度直方图重叠: 最终我可以将密度直方图转换为概率1:

a <- rnorm(1:100)
test <-hist(a,  plot=FALSE)
test$counts=(test$counts/sum(test$counts))*100   # Probability
plot(test, ylab="Probability")
curve(dnorm(x, mean=mean(a), sd=sd(a)), add=TRUE)

但是我不能重复正常的曲线,因为它会超出规模。

http://i57.tinypic.com/2qb9549.png

任何解决方案?也许第二个Y轴

3 个答案:

答案 0 :(得分:4)

现在我的问题很清楚了。实际上,第二个y轴似乎是最佳选择,因为这两个数据集具有完全不同的尺度。

为了做到这一点,你可以这样做:

set.seed(2)
a <- rnorm(1:100)
test <-hist(a,  plot=FALSE)
test$counts=(test$counts/sum(test$counts))*100   # Probability
plot(test, ylab="Probability")
#start new graph
par(new=TRUE)
#instead of using curve just use plot and create the data your-self
#this way below is how curve works internally anyway
curve_data <- dnorm(seq(-2, 2, 0.01), mean=mean(a), sd=sd(a))
#plot the line with no axes or labels
plot(seq(-2, 2, 0.01), curve_data, axes=FALSE, xlab='', ylab='', type='l', col='red' )
#add these now with axis
axis(4, at=pretty(range(curve_data)))

输出:

enter image description here

答案 1 :(得分:1)

首先,您应该保存您的rnorm数据,否则每次都会获得不同的数据。

seed = rnorm(100)

接下来继续

hist(seed,probability = T)
curve(dnorm(x, mean=mean(na.omit(seed)), sd=sd(na.omit(seed))), add=TRUE)

现在你有了预期的结果。具有密度曲线的直方图。

答案 2 :(得分:0)

y轴不是标记它的“概率”。它是计数数据。如果将直方图转换为概率,则不应该出现问题:

x <- rnorm(1000)
hist(x, freq= FALSE, ylab= "Probability")
curve(dnorm(x, mean=mean(x), sd=sd(x)), add=TRUE)

enter image description here

相关问题