向直方图和累积直方图添加密度线 [英] Add density lines to histogram and cumulative histogram
本文介绍了向直方图和累积直方图添加密度线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在直方图和累积直方图中添加密度曲线,如下所示:
I want to add density curve to histogram and cumulative histogram, like this:
这是我所能做到的:
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")
推荐答案
提供,无需解释:
## 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)
这篇关于向直方图和累积直方图添加密度线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文