R: Control number of histogram bins

☆樱花仙子☆ 提交于 2019-12-02 17:44:00

问题


I am using the hist-function to analyze some data I generated. For an analysis-assay I would like to precisely control the number of histogram bins.

I know the "break-argument" and I can see that in many cases the number of bins is in a direct relationship to the number of breaks (i.e. no_bins = no_breaks + 1).

Due to R's algorithm this is not always the case. Is there a way to force R to output a specific number of bins?

Let me know if I need to specify further details.

Best and many thanks!


回答1:


From ?hist, there are several options for controlling the bins through the breaks argument.

breaks one of:

a vector giving the breakpoints between histogram cells,

a function to compute the vector of breakpoints,

a single number giving the number of cells for the histogram,

a character string naming an algorithm to compute the number of cells (see ‘Details’),

a function to compute the number of cells.

In the last three cases the number is a suggestion only; the breakpoints will be set to pretty values. If breaks is a function, the x vector is supplied to it as the only argument.

For the greatest precision, you have to set the breakpoints exactly, either by supplying a vector of breakpoints, or a function to compute them. You need to cover the entire range of x with your breakpoints and there will be 1 more breakpoint than bins (i.e. no_bins + 1 = no_breaks).



来源:https://stackoverflow.com/questions/37183083/r-control-number-of-histogram-bins

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!