R 数据可视化 —— ggplot 直方图与密度图
2021-04-16 本文已影响0人
名本无名
1. 直方图
直方图是将单个变量分隔成若干个区间,并对区间内的观测值进行计数。
geom_histogram
函数可用于绘制直方图,
而它的变体 geom_freqpoly
使用线条来展示观测值数目。适用于比较分类变量的不同水平之间的分布差异
示例
ggplot(diamonds, aes(carat)) +
geom_histogram()
直方图默认分隔的是 10
个区间,可以通过设置 binwidth
参数覆盖该值
ggplot(diamonds, aes(carat)) +
geom_histogram(bins = 100)
也可以通过设置 binwidth
参数的值,该参数值会覆盖 bins
参数的值,所以只要设置其中一个参数就可以了
ggplot(diamonds, aes(carat)) +
geom_histogram(binwidth = 0.01)
可以将数据设置为 y
参数的值,更改朝向
ggplot(diamonds, aes(y = carat)) +
geom_histogram()
堆积直方图
ggplot(diamonds, aes(price, fill = cut)) +
geom_histogram(bins = 40)
我们可以使用 geom_freqpoly
来替代
ggplot(diamonds, aes(price, colour = cut)) +
geom_freqpoly(bins = 40)
或者绘制密度曲线,来比较不同水平的分布情况
ggplot(diamonds, aes(price, after_stat(density), colour = cut)) +
geom_freqpoly(bins = 40)
绘制镜像直方图
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")
多变量直方图
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"))
分面直方图
ggplot(diamonds, aes(price, fill = cut)) +
geom_histogram(alpha = 0.6, bins = 40) +
facet_wrap(~ cut) +
theme(legend.position = "none")
2. 密度图
密度图是直方图的平滑版本,用于计算并绘制数据的核密度估计,能够更好的界定分布的形状。
密度图绘制函数为 geom_density
示例
最简单的方式是绘制一条密度曲线
ggplot(diamonds, aes(carat)) +
geom_density()
设置 y
轴方向的密度曲线
ggplot(diamonds, aes(y = carat)) +
geom_density()
设置 adjust
参数的值,用于调整带宽,例如 1/5
或 5
是相对于默认值的 1/5
或 5
倍
ggplot(diamonds, aes(carat)) +
geom_density(adjust = 1/5)
ggplot(diamonds, aes(carat)) +
geom_density(adjust = 5)
设置分组密度图
ggplot(diamonds, aes(depth, colour = cut)) +
geom_density() +
xlim(55, 70)
设置填充色
ggplot(diamonds, aes(depth, fill = cut, colour = cut)) +
geom_density(alpha = 0.1) +
xlim(55, 70)
堆积密度图
ggplot(diamonds, aes(carat, fill = cut)) +
geom_density(position = "stack")
绘制堆积密度图,可能通常并不是想要看密度的堆积形式,而可能更想要看的是数量的堆积形式
ggplot(diamonds, aes(carat, after_stat(count), fill = cut)) +
geom_density(position = "stack")
百分比密度图
ggplot(diamonds, aes(carat, after_stat(count), fill = cut)) +
geom_density(position = "fill")
类似于直方图,我们也可以绘制镜像密度图
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")
分面密度图
ggplot(diamonds, aes(price, fill = cut)) +
geom_density(alpha = 0.6) +
facet_wrap(~ cut) +
theme(legend.position = "none")