我有两个不同长度的向量,每个向量都包含 0 到 50 之间的数字。一些数字不包含在向量中,其他数字可能会出现多次。
a <- c(22, 11, 9, 15, 19, 14, 13, 17, 24, 21, 21, 19, 20, 23, 18, 20, 21, 15, 25, 11, 21, 19, 27, 20, 30, 11, 31, 28, 21, 24, 29, 13, 19, 13, 18, 18, 11, 29, 7, 21, 36, 21, 31, 24, 28, 36, 12, 21, 18, 27, 6, 23, 22, 25, 17, 15, 27, 9, 33, 14, 4, 15, 31, 27, 22, 25, 31, 23, 8, 23, 27, 21, 19, 17, 5, 29, 15, 26, 25, 30, 29, 5, 19, 12, 23, 8, 21, 20, 23, 19, 18, 40, 33, 17, 15, 25, 15, 15, 10, 9, 19, 11, 41, 14, 33, 5, 28, 15, 27, 16, 9, 5, 21, 19, 19, 29, 30, 8, 15, 20, 26, 17, 35, 18, 18, 26, 35, 33, 30, 11, 26, 21, 14, 20, 20, 23, 15, 21, 23, 15, 12, 8, 33, 13, 15, 5, 19, 12, 23, 18, 19, 15, 18, 16, 7, 19, 21, 23, 8, 10, 6, 5, 20, 19, 18, 13, 32, 14, 11, 14, 26, 28, 20, 9, 31, 19, 9, 23, 29, 12, 37, 17, 15, 13, 18, 23, 18, 10, 13, 18, 28, 8, 17, 18, 14, 14, 19, 23, 16, 30, 16, 16, 19, 13, 15, 25, 22, 36, 8, 26, 5, 2, 26)
b <- c(7, 2, 3, 11, 16, 1, 3, 9, 15, 27, 2, 5, 11, 13, 24, 29, 11, 6, 1, 2, 5, 4, 2, 1, 7, 3, 0, 26, 13, 2, 15, 14, 11, 12, 15, 10, 4, 24, 21, 3, 43, 12, 19, 5, 2, 30, 9, 3, 5, 8, 25, 5, 24, 16, 15, 7, 2, 28, 8, 1, 15, 11, 3, 19, 28, 7, 3, 16, 7, 19, 5, 7, 1, 21, 21, 4, 8, 11, 16, 27, 13, 9, 2, 5, 14, 10, 3, 4, 20, 10, 7, 1, 10, 13, 11, 12, 10, 9, 24, 4, 26, 7, 11, 14, 3, 2, 9, 5, 1, 6, 9, 8, 16, 23, 3, 5, 5, 23, 25, 14, 3, 7, 16, 1, 11, 4, 2, 8, 6, 2, 2, 2, 2, 16, 8, 5, 15, 14, 10, 7, 9, 13, 5, 10, 18, 1, 24, 1, 8, 14, 3, 16, 18, 13, 0, 0, 10, 3, 21, 10, 8, 4, 2, 2, 4, 1, 10, 10, 8, 5, 17, 19, 2, 6, 5, 5, 17, 13, 0, 1, 19, 2, 14, 24, 7, 4, 8, 9, 7, 9, 6, 15, 8, 3, 7, 9, 13, 20, 13, 1, 7, 6, 28, 2, 11, 7, 0, 14)
我想绘制线条来显示每个数字包含在每个向量中的频率,即数字的频率。
如果我将中断设置为每个可能的数字之间,我可以绘制显示频率的直方图:
hist(a, breaks=seq(-0.5, 50.5, 1), xlim = c(0, 50), col = rgb(0,1,0,0.5))
par(new=TRUE)
hist(b, breaks=seq(-0.5, 50.5, 1), xlim = c(0, 50), col = rgb(1,0,1,0.5))
我知道有一个经验累积分布函数 ( ecdf()
),它会导致类似 S 的形状;但我想要的是一个非累积的 经验分布函数,它会产生类似于阶梯状钟形曲线的东西,类似于直方图的轮廓。
我能得到的最接近的是绘制密度:
plot(density(a), xlim = c(-10, 50))
par(new=TRUE)
plot(density(b), xlim = c(-10, 50))
但这不是我想要的。我想要的是频率“曲线”。它应该看起来像这样(在 Photohshop 中手绘):
那么如何才能实现频率“曲线”呢?