直方图不显示密度 [英] Histogram does not show densities
问题描述
我尝试了不同的功能和几个不同的参数,但直方图
I've tried different functions and several different arguments but the histogram
hist(estimator, probability=T, br=5)
不管我选择什么绘图函数或参数,
都不会在y轴上显示密度.向量估计量"包含100个介于0.4到0.6之间的值.
isn't showing densities on the y-axis no matter what plotting function or argument I choose. The vector "estimator" contains 100 values around between 0.4 and 0.6.
准确地说:创建历史对象,然后计算并更改密度并通过plot()再次进行绘制的方法,但是我不希望首先绘制历史对象:/
To be precise: the way by creating an hist object, then calculate and change the densities and plot it again by plot() works, but I don't want the hist object to be plotted in the first place :/
推荐答案
当您指定 probability = T
(或更好的是 probability = TRUE
)时,如果将 T
更改为除 TRUE
之外的其他内容,则会弄乱高度应略小于1,高度必须大于1,以使面积全部加到1.这使得叠加密度估算曲线或理论密度曲线或添加其他参考变得容易.
When you specify probability=T
(or better yet probability=TRUE
so that you don't get messed up if T
is changed to something besides TRUE
) is a scaling such that the entire area of the histogram bars add to 1, since the width of your bars is quite a bit less than 1 the heights need to be greater than 1 so that the areas all add to 1. This makes it easy to superpose a density estimate curve or a theoretical density curve or add other references.
通常,您应该只忽略y轴上的刻度标签(如果甚至不绘制它们,那会更好),它们只会分散图表的重要部分.
In general you should just ignore the tick labels on the y-axis (it would be better if they were not even plotted), they just distract from the important parts of the plot.
许多人认为他们希望y轴刻度标签代表每个分组中观察值的比例(或百分比)(这对于您自己的自定义轴是可能的),但是我认为这仍然是一种干扰.考虑一下如果更改直方图中条形/间隔的数量会发生什么,直方图的整体结构保持不变(前提是您不做大的改变),但是y轴上的刻度标签有时会更改因此,最好忽略它们(或者首先不要产生它们).
Many people think they want the y-axis tick labels to represent the proportion (or percentage) of observations within each grouping (and that is possible with your own custom axis), but I think this is still a distraction. Consider what happens if you change the number of bars/intervals in the histogram, the overall structure of the histogram stays the same (provided you don't make to drastic a change), but the tick labels on the y-axis change, sometimes by quite a bit, so they are better ignored (or not produced in the first place).
如果您真的认为需要百分比(或比例),那么代码就很简单:
If you really think that the percentages (or proportions) are needed then the code is as simple as:
x <- rgamma(327, 5, 3)
tmp <- hist(x, yaxt='n',ylab='Percent')
tmp2 <- pretty( tmp$counts/sum(tmp$counts)*100 )
axis(2, at=tmp2*sum(tmp$counts)/100, labels=tmp2)
如果需要,可以很容易地将其包装到函数中.
That could be easily wrapped into a function if you wanted.
这篇关于直方图不显示密度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!