R 数据可视化 —— ggplot 直方图与密度图

本文详细介绍了如何在R语言的ggplot2库中使用直方图和密度图进行数据可视化,包括直方图的基本用法、区间设置、堆积和分面,以及密度图的绘制、调整和分组等技巧。

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/55 是相对于默认值的 1/55

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

名本无名

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值