1. 直方图
直方图是将单个变量分隔成若干个区间,并对区间内的观测值进行计数。
geom_histogram
函数可用于绘制直方图,
而它的变体 geom_freqpoly
使用线条来展示观测值数目。适用于比较分类变量的不同水平之间的分布差异
示例
ggplot(diamonds, aes(carat)) +
geom_histogram()
![](https://img.haomeiwen.com/i18546936/c28216d78449768a.png)
直方图默认分隔的是 10
个区间,可以通过设置 binwidth
参数覆盖该值
ggplot(diamonds, aes(carat)) +
geom_histogram(bins = 100)
![](https://img.haomeiwen.com/i18546936/fd1e2e02858463f2.png)
也可以通过设置 binwidth
参数的值,该参数值会覆盖 bins
参数的值,所以只要设置其中一个参数就可以了
ggplot(diamonds, aes(carat)) +
geom_histogram(binwidth = 0.01)
![](https://img.haomeiwen.com/i18546936/e7320a5d60c93686.png)
可以将数据设置为 y
参数的值,更改朝向
ggplot(diamonds, aes(y = carat)) +
geom_histogram()
![](https://img.haomeiwen.com/i18546936/aef62e14beded34c.png)
堆积直方图
ggplot(diamonds, aes(price, fill = cut)) +
geom_histogram(bins = 40)
![](https://img.haomeiwen.com/i18546936/2d6b96bfff6270e5.png)
我们可以使用 geom_freqpoly
来替代
ggplot(diamonds, aes(price, colour = cut)) +
geom_freqpoly(bins = 40)
![](https://img.haomeiwen.com/i18546936/cddf8df21857615a.png)
或者绘制密度曲线,来比较不同水平的分布情况
ggplot(diamonds, aes(price, after_stat(density), colour = cut)) +
geom_freqpoly(bins = 40)
![](https://img.haomeiwen.com/i18546936/0bb2fa28080c0400.png)
绘制镜像直方图
data <- data.frame(
var1 = rnorm(1000),
var2 = rnorm(1000, mean=2)
)
ggplot(data, aes(x=x) ) +
# Top
geom_histogram(aes(x = var1, y = after_stat(density)), fill="#69b3a2" ) +
geom_label(aes(x=4.5, y=0.25, label="variable1"), color="#69b3a2") +
# Bottom
geom_histogram( aes(x = var2, y = -after_stat(density)), fill= "#404080") +
geom_label(aes(x=4.5, y=-0.25, label="variable2"), color="#404080")
![](https://img.haomeiwen.com/i18546936/23385b37abf79cc2.png)
多变量直方图
tibble(
type = c(rep("variable 1", 1000), rep("variable 2", 1000)),
value = c(rnorm(1000), rnorm(1000, mean=4))
) %>%
ggplot(aes(x=value, fill=type)) +
geom_histogram(color="#e9ecef", alpha=0.6, position = 'identity') +
scale_fill_manual(values=c("#377eb8", "#4daf4a"))
![](https://img.haomeiwen.com/i18546936/c86a7ca8548d1351.png)
分面直方图
ggplot(diamonds, aes(price, fill = cut)) +
geom_histogram(alpha = 0.6, bins = 40) +
facet_wrap(~ cut) +
theme(legend.position = "none")
![](https://img.haomeiwen.com/i18546936/620f3b81f70692e2.png)
2. 密度图
密度图是直方图的平滑版本,用于计算并绘制数据的核密度估计,能够更好的界定分布的形状。
密度图绘制函数为 geom_density
示例
最简单的方式是绘制一条密度曲线
ggplot(diamonds, aes(carat)) +
geom_density()
![](https://img.haomeiwen.com/i18546936/efd2108fe4946c51.png)
设置 y
轴方向的密度曲线
ggplot(diamonds, aes(y = carat)) +
geom_density()
![](https://img.haomeiwen.com/i18546936/d203d573090814d7.png)
设置 adjust
参数的值,用于调整带宽,例如 1/5
或 5
是相对于默认值的 1/5
或 5
倍
ggplot(diamonds, aes(carat)) +
geom_density(adjust = 1/5)
ggplot(diamonds, aes(carat)) +
geom_density(adjust = 5)
![](https://img.haomeiwen.com/i18546936/1fe126bebe01a354.png)
设置分组密度图
ggplot(diamonds, aes(depth, colour = cut)) +
geom_density() +
xlim(55, 70)
![](https://img.haomeiwen.com/i18546936/0a478a14270bb06b.png)
设置填充色
ggplot(diamonds, aes(depth, fill = cut, colour = cut)) +
geom_density(alpha = 0.1) +
xlim(55, 70)
![](https://img.haomeiwen.com/i18546936/3ff96c7e86e95ef3.png)
堆积密度图
ggplot(diamonds, aes(carat, fill = cut)) +
geom_density(position = "stack")
![](https://img.haomeiwen.com/i18546936/b1c81d8bab02ec3e.png)
绘制堆积密度图,可能通常并不是想要看密度的堆积形式,而可能更想要看的是数量的堆积形式
ggplot(diamonds, aes(carat, after_stat(count), fill = cut)) +
geom_density(position = "stack")
![](https://img.haomeiwen.com/i18546936/aaea0c3df0de144d.png)
百分比密度图
ggplot(diamonds, aes(carat, after_stat(count), fill = cut)) +
geom_density(position = "fill")
![](https://img.haomeiwen.com/i18546936/0cbae65121328332.png)
类似于直方图,我们也可以绘制镜像密度图
data <- data.frame(
var1 = rnorm(1000),
var2 = rnorm(1000, mean=2)
)
ggplot(data, aes(x=x) ) +
# Top
geom_density(aes(x = var1, y = after_stat(density)), fill="#69b3a2" ) +
geom_label(aes(x=4.5, y=0.25, label="variable1"), color="#69b3a2") +
# Bottom
geom_density( aes(x = var2, y = -after_stat(density)), fill= "#404080") +
geom_label(aes(x=4.5, y=-0.25, label="variable2"), color="#404080")
![](https://img.haomeiwen.com/i18546936/f73ea742cd1d8cb3.png)
分面密度图
ggplot(diamonds, aes(price, fill = cut)) +
geom_density(alpha = 0.6) +
facet_wrap(~ cut) +
theme(legend.position = "none")
![](https://img.haomeiwen.com/i18546936/0e01f935390e5787.png)
网友评论