hist 用于繪制直方圖烦租,下面介紹每個參數(shù)的作用尸红;
1)x: 用于繪制直方圖的數(shù)據(jù)歹撒,該參數(shù)的值為一個向量
代碼示例:
data <- c(rep(1, 10), rep(2, 5), rep(3, 6))
hist(data)
效果圖如下:
從圖中可以看出榨呆,橫坐標(biāo)為不同的區(qū)間荒给,縱坐標(biāo)為落入該區(qū)間內(nèi)的頻數(shù)倦炒;
2) break : 該參數(shù)的指定格式有很多種
第一種: 指定一個向量显沈,給出不同的斷點(diǎn)
代碼示例:
data <- c(rep(1, 10), rep(2, 5), rep(3, 6))
hist(data, breaks = c(0.5, 1.5, 2.5, 3.5))
效果圖如下:
第二種:指定分隔好的區(qū)間的個數(shù),會根據(jù)區(qū)間個數(shù)自動去計(jì)算區(qū)間的大小
代碼示例:
3)freq: 邏輯值,默認(rèn)值為TRUE , y軸顯示的是每個區(qū)間內(nèi)的頻數(shù)拉讯,F(xiàn)ALSE, 代表顯示的是頻率(= 頻數(shù)/ 總數(shù))
代碼示例:
par(mfrow = c(1, 2))
data <- c(rep(1, 10), rep(2, 5), rep(3, 6))
hist(data, breaks = c(0.5, 1.5, 2.5, 3.5), freq = T, main = "freq = T")
hist(data, breaks = c(0.5, 1.5, 2.5, 3.5), freq = F, main = "freq = F")
效果圖如下:
4)probability : 邏輯值涤浇,和 freq 參數(shù)的作用正好相反,TRUE 代表頻率魔慷, FALSE 代表頻數(shù)
代碼示例:
par(mfrow = c(1, 2))
data <- c(rep(1, 10), rep(2, 5), rep(3, 6))
hist(data, breaks = c(0.5, 1.5, 2.5, 3.5), probability = T, main = "probability = T")
hist(data, breaks = c(0.5, 1.5, 2.5, 3.5), probability = F, main = "probability = F")
效果圖如下:
5) labels: 顯示在每個柱子上方的標(biāo)簽只锭,
代碼示例:
hist(data, breaks = c(0.5, 1.5, 2.5, 3.5), labels = c("A", "B", "C"))
效果圖如下:
6) axes : 邏輯值,是否顯示軸線
代碼示例:
par(mfrow = c(1, 2))
data <- c(rep(1, 10), rep(2, 5), rep(3, 6))
hist(data, breaks = c(0.5, 1.5, 2.5, 3.5), axes = T, main = "axes = T")
hist(data, breaks = c(0.5, 1.5, 2.5, 3.5), axes = F, main = "axes = F")
效果圖如下:
7) col : 柱子的填充色
代碼示例:
par(mfrow = c(1, 2))
data <- c(rep(1, 10), rep(2, 5), rep(3, 6))
hist(data, breaks = c(0.5, 1.5, 2.5, 3.5), col = "pink")
hist(data, breaks = c(0.5, 1.5, 2.5, 3.5), col = rainbow(3))
效果圖如下:
8) border : 柱子的邊框的顏色,默認(rèn)為black, 當(dāng)border = NA 時呼巴, 代表沒有邊框
代碼示例:
hist(data, breaks = c(0.5, 1.5, 2.5, 3.5), col = rainbow(3), border = NA)
效果圖如下:
9) densitty 和 angle , 用線條填充柱子
代碼示例: density 控制填充的線條的密度
par(mfrow = c(1, 3))
data <- c(rep(1, 10), rep(2, 5), rep(3, 6))
hist(data, breaks = c(0.5, 1.5, 2.5, 3.5),? density = 1, main? = "density = 1")
hist(data, breaks = c(0.5, 1.5, 2.5, 3.5),? density = 2, main? = "density = 2")
hist(data, breaks = c(0.5, 1.5, 2.5, 3.5),? density = 3, main? = "density = 3")
效果圖如下:
代碼示例: angle 控制線條的角度虚吟,必須和density 參數(shù)配合使用,才能發(fā)揮作用
par(mfrow = c(1, 3))
data <- c(rep(1, 10), rep(2, 5), rep(3, 6))
hist(data, breaks = c(0.5, 1.5, 2.5, 3.5),? density = 2, angle = 45,? main? = "angle = 45")
hist(data, breaks = c(0.5, 1.5, 2.5, 3.5),? density = 2, angle = 90,? main? = "angle = 90")
hist(data, breaks = c(0.5, 1.5, 2.5, 3.5),? density = 2, angle = 180, main? = "angle = 180")
效果圖如下:
最后介紹一下hist函數(shù)的返回值
data <- c(rep(1, 10), rep(2, 5), rep(3, 6))
a <- hist(data, breaks = c(0.5, 1.5, 2.5, 3.5))
a
$breaks
[1] 0.5 1.5 2.5 3.5
$counts
[1] 10? 5? 6
$density
[1] 0.4761905 0.2380952 0.2857143
$mids
[1] 1 2 3
$xname
[1] "data"
$equidist
[1] TRUE
attr(,"class")
[1] "histogram"
從代碼中的結(jié)果可以看到铺呵,返回值是一個 histogram 類型的對象, 其中breaks 是分隔的區(qū)間隧熙,counts 是每個區(qū)間的頻數(shù)片挂,density是每個區(qū)間的頻率,mids 是每個柱子的中心點(diǎn)贞盯;
利用返回值音念,我們可以用hist函數(shù)統(tǒng)計(jì)一串?dāng)?shù)據(jù)在不同區(qū)間的頻數(shù)分布
摘抄自 廬州月光