繪製在該R,可以繪製直方圖和保存它的屬性對變量:變量影響直方圖HIST()函數中的R
> h1=hist(c(1,1,2,3,4,5,5), breaks=0.5:5.5)
這些屬性可以接着讀:
> h1
$breaks
[1] 0.5 1.5 2.5 3.5 4.5 5.5
$counts
[1] 2 1 1 1 2
$density
[1] 0.2857143 0.1428571 0.1428571 0.1428571 0.2857143
$mids
[1] 1 2 3 4 5
$xname
[1] "c(1, 1, 2, 3, 4, 5, 5)"
$equidist
[1] TRUE
attr(,"class")
[1] "histogram"
這些屬性如何影響直方圖?到目前爲止,我已經計算出以下內容:
$breaks
和$counts
之間的關係。 $breaks
表示的時間間隔,其中標繪數據可以落入和$counts
表示落入這個間隔中的數據的量,例如:
[]表示closed interval(包括端點)
()表示open interval(端點不包括在內)
BREAKS : COUNTS
[0.5-1.5] : 2 # There are two 1 which falls into this interval
(1.5-2.5] : 1 # There is one 2 which falls into this interval
(2.5-3.5] : 1 # There is one 3 which falls into this interval
(3.5-4.5] : 1 # There is one 4 which falls into this interval
(4.5-5.5] : 2 # There are two 5 which falls into this interval
$breaks
和$density
之間的關係基本上是與上述相同,但寫入百分比,例如:
BREAKS : DENSITY
[0.5-1.5] : 0.2857143 # This interval covers cca 28% of plot
(1.5-2.5] : 0.1428571 # This interval covers cca 14% of plot
(2.5-3.5] : 0.1428571 # This interval covers cca 14% of plot
(3.5-4.5] : 0.1428571 # This interval covers cca 14% of plot
(4.5-5.5] : 0.2857143 # This interval covers cca 28% of plot
當然,當你總結所有這些值,你將獲得1:
> sum(h1$density)
[1] 1
繼代表x軸的名字:
$xname
[1] "c(1, 1, 2, 3, 4, 5, 5)"
但到底是什麼做的,其餘做,尤其是$mids
?
$mids
[1] 1 2 3 4 5
$equidist
[1] TRUE
attr(,"class")
[1] "histogram"
另外,help(hist)
回報許多人一樣,不應該他們也列在上面的輸出,如果沒有,爲什麼?正如在following文章
默認情況下解釋,箱數包括值小於或等於斌的 右路突破點和嚴格比斌的左路突破 點時,除了最左邊的垃圾桶,其中包括其左破 點。
所以以下:
h1=hist(c(1,1,2,3,4,5,5,1.5), breaks=0.5:5.5)
將返回直方圖,其中1.5將落入0.5-1.5區間。一種「解決方法」是將間隔尺寸設置爲例如小於
h1=hist(c(1,1,2,3,4,5,5,1.5), breaks=seq(0.5,5.5,0.1))
但這似乎不現實的我,這也增加了$counts
和$density
一串0,有沒有更好的,自動的方式?
除了這個,它也有一個副作用,我不能解釋自己:爲什麼最後一個例子在總結10而不是1返回?
> sum(h1$density)
[1] 10
> h1$density[h1$density>0]
[1] 2.50 1.25 1.25 1.25 1.25 2.50
請閱讀'?[functionname]'張貼這樣的問題了。 –