按支付方式比较车费分布
我们已经看到现金支付没有小费。这是否意味着用现金支付的人不付小费,还是意味着现金支付时没有记录小费?对于相似的路线,我们预期无论支付方式如何,总车费的分布应当相同。在本练习中,我们将创建一个分位数图,按支付方式比较总车费的分布,并在不同的小面板中与原始图进行对比。
我们已为您创建了数据集 amount_compare,其中包含变量 payment_type、amount 和 amount_type。amount_type 用于区分表示总支付金额的取值与不含小费的支付金额。
本练习是课程的一部分
使用 R 中的 Trelliscope 可视化大数据
练习说明
- 在完成练习前,先将
amount_compare数据集打印到控制台以检查其结构,帮助您明确绘图规范。 - 使用
geom_qq()为总车费金额amount创建分位数图。请务必指定distribution = stats::qunif。 - 通过按支付方式设置
color,为每种支付方式分别绘制分位数图,使各自的分布叠加在同一幅图上。 - 通过分面展示每个
amount_type的分布对比。
交互式实操练习
通过完成这段示例代码来试试这个练习。
library(ggplot2)
library(dplyr)
library(tidyr)
# Get data ready to plot
amount_compare <- tx_pop %>%
mutate(total_no_tip = total_amount - tip_amount) %>%
select(total_amount, total_no_tip, payment_type) %>%
pivot_longer(!payment_type, names_to = "amount_type", values_to = "amount")
# Quantile plot
ggplot(___, aes(sample = ___, color = ___)) +
geom_qq(distribution = ___, shape = 21) +
facet_wrap(~ ___) +
ylim(c(3, 20))