R /のHIST / hist()関数について誰かが私を見つけるのを手伝ってくれますか?
'breaks'の指定された値とヒストグラムで生成されたビンの数との関係を説明する非常に単純な定義?
たとえば、Rツールで提供される基本的なデータセットを使用します。
data(mtcars)
hist(mtcars$mpg, break=3) --> will draw 3 bins (really??? weird!)
hist(mtcars$mpg, break=4) --> will draw 5 bins
hist(mtcars$mpg, break=5) --> will draw 5 bins no change, same as breaks=4
hist(mtcars$mpg, break=6) --> will draw 5 bins no change, same as breaks=4
hist(mtcars$mpg, break=7) --> will draw 5 bins no change, same as breaks=4
hist(mtcars$mpg, break=8) --> will draw 5 bins no change, same as breaks=4
hist(mtcars$mpg, break=9) --> will draw 11 bins (why???)
なぜbreaks = 4,5,6,7,8は同じ数のビンになり、breaks = 3はたった4つのビンになります...?
?histまたは次のリンクにあるRドキュメント:http://localhost//library/graphics/html/hist.html
あまり役に立たなかったので、「breaks =」で指定された値、ビンのサイズ、ビンの数の間にリンクを確立しようとしましたが、そのような「リンク」を差し引くための簡単または単純な式や説明が見つかりませんでした。
「breaks = 3」とはどういう意味かわかりませんか?「3回休憩」という意味ですか、それとも「3ユニットおきに休憩」という意味ですか、それともまったく違うものですか?
ヒント、ヘルプ、あらゆる種類のポインタをいただければ幸いです。
ありがとうございました。
のドキュメントにhist
は、ブレークを単一の数値として指定すると(行ったように)、
数字は提案にすぎません。ブレークポイントがきれいな値に設定されるため
以下のためにあなたは、ドキュメントへのリンクをたどっている場合pretty
、それは言います
値は、10の累乗の1、2、または5倍になるように選択されます。
1、2、5、または10の4つの等間隔の倍数で10と35の間のギャップにまたがることはできないため、5つのビン(6つのブレークポイント)を選択しました。本当に4つの等間隔のビンが必要な場合は、次を使用できます。
hist(mtcars$mpg, seq(10,35, length.out=5))
4つのビン(4つの開始点と1つの追加の終了点)を取得するには、length.out = 5を使用する必要があることに注意してください。もちろん、これは「きれいな」値を与えるものではありません。
x軸の目盛りがビンと整列しないのが気に入らない場合は(私はそうは思いません)、軸hist
を省略して自分で追加することができます。
H = hist(mtcars$mpg, seq(10,35, length.out=5), axes=FALSE, ylim=c(0,14))
axis(side=1, at=seq(10,35, length.out=5))
axis(side=2, pretty(0:14))
breaks
ドキュメントに?hist
は、休憩の下に、休憩に使用できる5種類の値があると記載されています。使用しているものは次のとおりです。
ヒストグラムのセル数を示す単一の数値
しかし、上記のように、ドキュメントは次のように追加します。
the number is a suggestion only; the breakpoints will be set to pretty values.
So when you give hist
the argument breaks=4
, it knows you want 4 bins, but it will also insist on using "pretty" values for the boundaries, that is, evenly spaced multiples of 1,2, 5 times a power of ten. There may also be constraints on the endpoints.
Let's investigate what it does with your mtcars$mpg data. You can get a lot of information about what hist
is doing by saving the return value. I will also suppress the actual plotting of the histogram since right now I am only interested in the value.
HV = hist(mtcars$mpg, 4, plot=FALSE)
You can print out HV and see that there is a lot of information about the histogram. All we care about here is stored in breaks
.
HV$breaks
[1] 10 15 20 25 30 35
This is giving the 6 boundary values for the bins (5 bins need 6 boundary values). But we asked for 4 bins, not 5! If you split the range 10-35 into four bins you get the boundaries 10, 16.25, 22.5, 28.75 and 35. These are not "pretty" boundary values. Instead, hist
uses the pretty
function to find nicer values for the boundaries, but that means it has to give up using 4 bins.
How many break points do we get for a range of values of breaks? Let's try 2 breaks up to 20 breaks.
sapply(2:20, function(n)
length(hist(mtcars$mpg, n, plot=FALSE)$breaks))
[1] 4 4 6 6 6 6 6 13 13 13 13 13 13 13 13 25 25 25 25
Note again: 4 break points means 3 bins. 6 break points means 5 bins. There are only four different splits that are created. What are they?
unique(lapply(2:20, function(n) hist(mtcars$mpg, n, plot=FALSE)$breaks))
[[1]]
[1] 10 20 30 40
[[2]]
[1] 10 15 20 25 30 35
[[3]]
[1] 10 12 14 16 18 20 22 24 26 28 30 32 34
[[4]]
[1] 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
The boundaries change by 10, 5 2 or 1 - pretty boundaries.
より詳細に制御したい場合は、境界を配置する場所を指定できる必要があります。それは私が上の例でしたことです。指定する他のオプションの1つbreaks
は次のとおりです。
ヒストグラムセル間のブレークポイントを与えるベクトル
それは私が指定しseq(10,35, length.out=5)
たときに使用したものです。ただし、値に注意してください。
seq(10,35, length.out=5)
[1] 10.00 16.25 22.50 28.75 35.00
きれいではありません。
したがって、簡単できれいにすることができますが、ビンの数を適切に制御しなくても、またはより多くの作業と醜い境界を犠牲にしてビンの数を制御することができます。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加