gpt4 book ai didi

r : ecdf over histogram

转载 作者:行者123 更新时间:2023-12-03 18:19:32 24 4
gpt4 key购买 nike

在 R 中,使用 ecdf我可以绘制经验累积分布函数

plot(ecdf(mydata))

并与 hist我可以绘制数据的直方图
hist(mydata)

如何在同一个图中绘制直方图和 ecdf?

编辑

我试着做这样的事情

https://mathematica.stackexchange.com/questions/18723/how-do-i-overlay-a-histogram-with-a-plot-of-cdf

最佳答案

也有点晚了,这是另一个解决方案,它使用第二个 y 轴扩展 @Christoph 的解决方案。

par(mar = c(5,5,2,5))
set.seed(15)
dt <- rnorm(500, 50, 10)
h <- hist(
dt,
breaks = seq(0, 100, 1),
xlim = c(0,100))

par(new = T)

ec <- ecdf(dt)
plot(x = h$mids, y=ec(h$mids)*max(h$counts), col = rgb(0,0,0,alpha=0), axes=F, xlab=NA, ylab=NA)
lines(x = h$mids, y=ec(h$mids)*max(h$counts), col ='red')
axis(4, at=seq(from = 0, to = max(h$counts), length.out = 11), labels=seq(0, 1, 0.1), col = 'red', col.axis = 'red')
mtext(side = 4, line = 3, 'Cumulative Density', col = 'red')

Histogram with CDF, two scales and two y-axes

诀窍如下:您不要在绘图中添加一条线,而是在顶部绘制另一个绘图,这就是我们需要 par(new = T) 的原因。 .然后您必须稍后添加 y 轴(否则它将绘制在左侧的 y 轴上)。

积分去 here (@tim_yates 回答)和 there .

关于r : ecdf over histogram,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29289046/

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