我有一个十五分的评分系统[1 ... 15],其中的期望是:
绘制频率的直方图很容易,但我想在此直方图的顶部绘制几条曲线
我多年没用过R而且有点挣扎。我在这里:
grades <-c(6,6,5,5,8,8,8,8,9,9,9,9,1,5,5,5,5,5,5,5,13,15)
xbar <- mean(g)
sdev <- sd(g)
plotCurve <- function(discreteRangeMin, discreteRangeMax, targetMean, targetDev, color) {
granularityFactor <- 4
xfit <- seq(discreteRangeMin, discreteRangeMax, length=discreteRangeMax * granularityFactor)
yfit <- dnorm(xfit, mean = targetMean, sd = targetDev)
plot(xfit, yfit, type='l', axes=F, xlab='', ylab='', col=color)
return (yfit)
}
plotHist <- function(g, title) {
zeroThrough15 <- seq(0, 15, length = 16)
hist(g, breaks=zeroThrough15, freq=T, main=title )
axis(side=1, at=zeroThrough15, labels=zeroThrough15)
}
# main histogram
title <- paste('Distribution [mean=', round(xbar, 2) , '', ', sd=', round(sdev, 2), ']', sep='')
plotHist(grades, title)
# add the expected 'full' distribution over the entire range
par(new=T)
yfit <- plotCurve(1, 15, targetMean=8, targetDev=2, 'green')
axis(side=4, at = pretty(range(yfit)))
# add the expected 'practical' distribution over typical range
par(new=T)
yfit2 <- plotCurve(5, 10, targetMean=8, targetDev=0.5, 'red')
问题