gpt4 book ai didi

r - 向直方图和累积直方图添加密度线

转载 作者:行者123 更新时间:2023-12-03 14:46:58 24 4
gpt4 key购买 nike

我想将密度曲线添加到直方图和累积直方图,如下所示:

enter image description here

这是我所能做的:

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)

enter image description here

关于r - 向直方图和累积直方图添加密度线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13960896/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com