简单的直方图绘制错了吗? [英] Simple histogram plot wrong?

查看:64
本文介绍了简单的直方图绘制错了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

test <- rep(5,20)
hist(test,freq=FALSE,breaks=5)

向量包含值5的20倍.当我用freq=FALS E和breaks=5绘制该图时,我期望在高度= 1.0的x = 5处看到1 bar,因为值5构成了值的100%.数据.

The vector contains 20 times the value 5. When I plot this with freq=FALSE and breaks=5 I expect to see 1 bar at x=5 with height = 1.0, because the value 5 makes up 100% of the data.

为什么我反而看到1个条形,其范围从x = 0到x = 5且高度= 0.2 ??

Why do I instead see 1 bar that ranges from x=0 to x=5 and has height = 0.2 ??

推荐答案

hist绘制freq=FALSEprob=TRUE时概率密度的估计值,因此总区域直方图中的条形图总计为1.由于绘制的单个条形图的水平范围为(0,5),因此其高度必须为0.2(5 * 0.2 = 1)

hist plots an estimate of the probability density when freq=FALSE or prob=TRUE, so the total area of the bars in the histogram sums to 1. Since the horizontal range of the single bar that is plotted is (0,5), it follows that the height must be 0.2 (5*0.2=1)

如果您真的想要您想要的直方图(即,高度对应于计数的分数,面积不一定等于1),则可以执行以下操作:

If you really want the histogram you were expecting (i.e. heights correspond to fraction of counts, areas don't necessarily sum to 1), you can do this:

 h <- hist(test,plot=FALSE)
 h$counts <- h$counts/length(test)
 plot(h)

另一种可能性是强制钢筋宽度等于1.0,例如

Another possibility is to force the bar widths to be equal to 1.0, e.g.

 hist(test,freq=FALSE,breaks=0:10)

也许你想要

 plot(table(test)/length(test))

 plot(table(test)/length(test),lwd=10,lend="butt")

? 另请参阅:如何使用hist进行绘图R中的相对频率?

这篇关于简单的直方图绘制错了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆