《R数据可视化手册》学习笔记2---快速探索数据(4)直方图
2023-09-07 本文已影响0人
RSP小白之路
写在前面。
很多时候在处理数据前或者出图前,可能需要先对数据整体情况进行了解。这个时候我们可以用到R基础绘图的语句
和ggplot2
完成目标。
接下来,我们分不同的图形类型
进行啃书学习。
4. 绘制直方图
如何绘制直方图来查看一维数据的分布情况?
- 使用R基础绘图系统
使用hist
函数绘制直方图,传递一个向量:
hist(mtcars$mpg)
![](https://img.haomeiwen.com/i18003060/7d28e3dd3b236847.jpeg)
使用breaks
参数指定大致组距:
hist(mtcars$mpg, breaks = 10)
![](https://img.haomeiwen.com/i18003060/103ac22f8583ee5c.jpeg)
- 使用ggplot2的
qplot
函数
qplot(mtcars$mpg)
![](https://img.haomeiwen.com/i18003060/f571fdf848ae6e68.jpeg)
binwidth
指定组距,下面两段代码等价:
qplot(mtcars$mpg, binwidth = 4)
dev.off()
ggplot(mtcars, aes(mpg) )+ geom_histogram(binwidth = 4)
![](https://img.haomeiwen.com/i18003060/4270ae79e39b139e.jpeg)
以上。