# R语言hist绘图函数

hist 用于绘制直方图，下面介绍每个参数的作用；

1）x: 用于绘制直方图的数据，该参数的值为一个向量

```data <- c(rep(1, 10), rep(2, 5), rep(3, 6))
hist(data)```

2） break : 该参数的指定格式有很多种

```data <- c(rep(1, 10), rep(2, 5), rep(3, 6))
hist(data, breaks = c(0.5, 1.5, 2.5, 3.5))```

3）freq: 逻辑值，默认值为TRUE , y轴显示的是每个区间内的频数，FALSE, 代表显示的是频率（= 频数/ 总数）

```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 参数的作用正好相反，TRUE 代表频率， FALSE 代表频数

```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: 显示在每个柱子上方的标签，

`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 : 柱子的边框的颜色，默认为black, 当border = NA 时， 代表没有边框

`hist(data, breaks = c(0.5, 1.5, 2.5, 3.5), col = rainbow(3), border = NA)`

9） densitty 和 angle , 用线条填充柱子

```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")```

```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")```

```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"```

R语言hist绘图函数

(0)
(14)

0条