R 将密度线添加到直方图和累积直方图
我想在直方图和累积直方图中添加密度曲线,如下所示: 以下是我能做的:R 将密度线添加到直方图和累积直方图,r,graph,plot,base,kernel-density,R,Graph,Plot,Base,Kernel Density,我想在直方图和累积直方图中添加密度曲线,如下所示: 以下是我能做的: hist.cum <- function(x, plot=TRUE, ...){ h <- hist(x, plot=FALSE, ...) h$counts <- cumsum(h$counts) h$density <- cumsum(h$density) h$itensities <- cumsum(h$itensities) if(plot) plot(h)
hist.cum <- function(x, plot=TRUE, ...){
h <- hist(x, plot=FALSE, ...)
h$counts <- cumsum(h$counts)
h$density <- cumsum(h$density)
h$itensities <- cumsum(h$itensities)
if(plot)
plot(h)
h
}
x <- rnorm(100, 15, 5)
hist.cum(x)
hist(x, add=TRUE, col="lightseagreen")
#
lines (density(x), add = TRUE, col="red")
hist.cum提供,无需解释:
## Make some sample data
x <- sample(0:30, 200, replace=T, prob=15 - abs(15 - 0:30))
## Calculate and plot the two histograms
hcum <- h <- hist(x, plot=FALSE)
hcum$counts <- cumsum(hcum$counts)
plot(hcum, main="")
plot(h, add=T, col="grey")
## Plot the density and cumulative density
d <- density(x)
lines(x = d$x, y = d$y * length(x) * diff(h$breaks)[1], lwd = 2)
lines(x = d$x, y = cumsum(d$y)/max(cumsum(d$y)) * length(x), lwd = 2)
##制作一些示例数据
x密度
与频率
的比例不同。我很肯定你会在中找到有用的例子,如果你再多搜索一点的话。你在发帖前就搜索过了,对吧?你需要多少副本@谢谢你的建议,我都看过了,但我不知道如何叠加累积密度曲线和规则密度曲线。。。