惡名昭彰的 P-I-E
直覺來看,圓餅圖就像是把堆疊長條圖「繞」著某個中心軸包起來。剛好,這個直覺也很符合在 ggplot2 中的作法。
以下提供的程式碼會把 who_disease 資料整理成一個資料框,包含三種疾病:measles、mumps 和 other,以及它們在資料中的病例總數。
你的任務是把空的 ggplot 物件先變成堆疊長條圖,接著使用轉換 coord_polar(theta = 'y') 把它變成圓餅圖。
注意我在對映參數中設定了 x = 1。這是因為我們只需要一個長條圖。下一個單元我們會學到多個堆疊長條圖!
本練習屬於課程
R 視覺化最佳實務
練習說明
- 在提供的 ggplot 物件中加入長條幾何圖層(
geom_col())。 - 加上
coord_polar()以切換到極座標。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Wrangle data into form we want.
disease_counts <- who_disease %>%
mutate(disease = ifelse(disease %in% c('measles', 'mumps'), disease, 'other')) %>%
group_by(disease) %>%
summarise(total_cases = sum(cases))
ggplot(disease_counts, aes(x = 1, y = total_cases, fill = disease)) +
# Use a column geometry.
___
# Change coordinate system to polar and set theta to 'y'.
___