5 篇文章 37 订阅

# 一、直方图hist()参数简介

**函数：** hist(x, breaks = "Sturges", freq = NULL, probability = !freq, include.lowest = TRUE, right = TRUE, density = NULL, angle = 45, col = "lightgray", border = NULL, main = paste("Histogram of" , xname), xlim = range(breaks), ylim = NULL, xlab = xname, ylab, axes = TRUE, plot = TRUE, labels = FALSE, nclass = NULL, warn.unused = TRUE, ...) 参数： x：数据（不多谈，一个向量，用来绘制直方图的数据） main：直方图标题。（不多谈）

a <- rnorm(50,10,5)


# 二、hist()参数详解

## 1.breaks

1.breaks为数字向量时（常用）：指定直方图在哪些点截断，一定要包含数据的极值，否则会报错
2.breaks为单个数字时：指定直方图有多少个直方（会有偏差）
3.breaks可以为函数或者字符串值，用来计算直方图的断点

par(mfrow = c(1,2))
hist(a,main = "breaks = c(min(a),max(a))",breaks = c(min(a),max(a)))
hist(a,main = "breaks = c(min(a),1,max(a))",breaks = c(min(a),1,max(a)))


## 2.freq

freq：设置直方图纵轴时表示频数还是概率密度，FALSE展示概率密度，默认为FALSE

par(mfrow = c(1,2))
hist(a,main = "freq = T",freq = T)
hist(a,main = "freq = F",freq = F)


## 3.probability

hist(a,main = "probability = T",probability = T)
hist(a,main = "probability = F",probability = F)


## 5.right

a <- c(1,2,3,5,6,7)
hist(a,breaks = c(0,1,2,3,4,5,6,7,8),main = "right = T",right = T)
hist(a,breaks = c(0,1,2,3,4,5,6,7,8),main = "right = F",right = F)


## 6.density，angle

a <- c(1,2,3,5,6,7)
hist(a,breaks = c(0,1,2,3,4,5,6,7,8),main = "density = c(10,50)\nangle = 30",density = c(10,50),angle = 30)
hist(a,breaks = c(0,1,2,3,4,5,6,7,8),main = "density = c(10,50)\nangle = 120",density = c(10,50),angle = 120)


## 7.col，border

a <- c(1,2,3,5,6,7)
hist(a,breaks = c(0,1,2,3,4,5,6,7,8),main = "col = c('red','lightblue')",col = c('red','lightblue'))
hist(a,breaks = c(0,1,2,3,4,5,6,7,8),main = "border = c('lightblue','red')",border = c('lightblue','red'),col = NULL)


## 8.labels

1.labels=TRUE，这时候每一个直方图上面的数字代表了，频数（左上图）
2.labels为一个字符向量时，相当于在直方图上加注释

• 如果这个这个字符串向量长度大于直方的个数，那么会继续从前往后添加标签直至向量结束（右上图），出现覆盖的情况
• 如果这个字符串向量长度小于直方的个数，那么会继续从向量的开头取读取标签直至所有的直方都有一个标签，也就是循环读取labels向量（右下图）

代码如下：
par(mfrow=c(2,2))
a <- c(1,2,2,5,6,7,6,6,6,7,7)
names(a) <- c('a','b','c','d','e','f','g','h','i','j','k')
hist(a,breaks = c(0,1,2,3,4,5,6,7,8),main = 'labels = T',labels = T,ylim = c(0,max(table(a))+1))
hist(a,breaks = c(0,1,2,3,4,5,6,7,8),main = 'labels = names(a)',labels = names(a),ylim = c(0,max(table(a))+1))
b <- c(1,2,3,4,5,6,7,8)
names(b) <- c('a','b','c','d','e','f','g','h')
hist(b,breaks = c(0,1,2,3,4,5,6,7,8),main = 'labels = names(b)',labels = names(b),ylim = c(0,max(table(b))+1))
hist(b,breaks = c(0,1,2,3,4,5,6,7,8),main = 'labels = names(b)[1:4]',labels = names(b)[1:4],ylim = c(0,max(table(b))+1))



plot：即是否显示图

# 总结

• 27
点赞
• 211
收藏
• 打赏
• 1
评论
03-26
01-02 10万+
06-09 2万+
03-20 769
03-01 533
07-16 1万+
02-18 1万+
09-01 6525
11-28 633
11-22 3578
03-19 229
05-01 340
01-14 266

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。