使用 stat_sum
在 Vocab 資料集裡,education 與 vocabulary 都是整數變數。你在第一門課已經看到,這是造成過度重疊繪圖(overplotting)的四大原因之一。兩個變數的每個交會點都只會出現單一點。
第 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)