开发者

How do I plot the densities from a histogram?

开发者 https://www.devze.com 2023-04-10 09:26 出处:网络
So I want to do something like this in R: x <- rnorm(1000, 100, 50) h <- hist(x, breaks=\"fd\")

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?


Turns out all I needed to do was to set the freq field to FALSE

So I just did hist(rnorm(1000, 100, 50), freq="FALSE") and that did a histogram of relative frequencies.


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:

plot(h$breaks[-length(h$breaks)] + 5, h$density)

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号