使用 stat_sum
在 Vocab 数据集里,education 和 vocabulary 都是整数变量。您在第一门课程中已经看到,这正是导致过度绘制的四个原因之一。两变量的每个交点都会只绘制一个点。
一种解决方案(在步骤 1 中展示)是配合透明度进行抖动(jitter)。另一种方法是使用 stat_sum(),它会计算重叠观测的总数,并将其映射到 size 美学属性上。
stat_sum() 允许使用一个特殊变量 ..prop..,用来显示数据集中取值所占的"比例"。
本练习是课程的一部分
ggplot2 数据可视化进阶
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Run this, look at the plot, then update it
ggplot(Vocab, aes(x = education, y = vocabulary)) +
# Replace this with a sum stat
geom_jitter(alpha = 0.25)