比較大量分佈
讓我們回到先前做過的分面圖,但這次用上剛學到的新技巧。用新的圖形類型,能不能更清楚掌握變數之間的關係?
提供的程式碼會產生上一節課相同的視覺化。把程式改成用小提琴圖呈現密度,而不是用抖動圖顯示個別資料點。和上一個練習一樣,把盒狀圖的寬度縮小,讓它大多落在小提琴圖內。最後,別忘了在圖上加上副標題,告訴讀者你的小提琴圖 kernel 寬度!
本練習屬於課程
R 視覺化最佳實務
練習說明
- 將
geom_jitter()換成geom_violin()。 - 對小提琴幾何設定
fill = 'steelblue',並將 kernel 的標準差設為2.5。 - 將
geom_boxplot()的width縮小為0.3。 - 加上副標題
Gaussian kernel width: 2.5'。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
md_speeding %>%
ggplot(aes(x = gender, y = speed)) +
# replace with violin plot with kernel width of 2.5, change color argument to fill
geom_jitter(alpha = 0.3, color = 'steelblue') +
# reduce width to 0.3
geom_boxplot(alpha = 0) +
facet_wrap(~vehicle_color) +
labs(
title = 'Speed of different car colors, separated by gender of driver'
# add a subtitle w/ kernel width
)