如何从直方图中绘制密度?
所以我想在 R 中做这样的事情:
x <- rnorm(1000, 100, 50)
h <- hist(x, breaks="fd")
z <- plot(h$breaks, h$density)
问题是直方图中的 $breaks 字段比 $ Density 字段多一个值?有一个简单的方法可以解决这个问题吗?
So I want to do something like this in R:
x <- rnorm(1000, 100, 50)
h <- hist(x, breaks="fd")
z <- plot(h$breaks, h$density)
The problem is that the $breaks field in the histogram has one more value than the $density field? is there an easy way around this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
结果我需要做的就是将
freq
字段设置为FALSE
所以我只是做了
hist(rnorm(1000, 100, 50), freq=" FALSE")
并绘制了相对频率的直方图。Turns out all I needed to do was to set the
freq
field toFALSE
So I just did
hist(rnorm(1000, 100, 50), freq="FALSE")
and that did a histogram of relative frequencies.我不确定到底是什么问题,但是您可以删除 h$breaks 的第一个或最后一个元素来绘制任一端点处的点,或者您可以删除最后一个元素,然后添加一半的 bin 宽度以将它们绘制在中点:
不过,这只是解决了您的特定问题。如果您扩展一下您想要做的事情,那么通常可能有更好的使用
hist
的方法。I'm not sure exactly what the problem is, but you could just drop either the first or last element of
h$breaks
to plot the points at either endpoint, or you could drop the last element and then add half the bin width to plot them at the midpoints:That just fixes your specific problem, though. There may be a better way of using
hist
in general, if you expanded on what you're trying to do a bit.