基本堆疊長條圖
在前面的練習中製作圓餅圖與 waffle 圖時,我們把所有年份的資料合併在一起看。
現在,我們想觀察這些型態隨時間的變化。為此,我們會建立一張堆疊長條圖,將觀測年份放在 x 軸上。就像先前做圓餅圖一樣,我們會把資料簡化為 measles、mumps,以及 other。
用這種方式視覺化之後,你有注意到資料中有哪些不太尋常的地方嗎?
本練習屬於課程
R 視覺化最佳實務
練習說明
- 修改
aes(),把年份對應到 x 軸。 - 在
geom_col()中調整position參數,讓長條填滿整個 y 軸。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
disease_counts <- who_disease %>%
mutate(disease = ifelse(disease %in% c('measles', 'mumps'), disease, 'other')) %>%
group_by(disease, year) %>% # note the addition of year to the grouping.
summarise(total_cases = sum(cases))
# add the mapping of year to the x axis.
ggplot(disease_counts, aes(___, y = total_cases, fill = disease)) +
# Change the position argument to make bars full height
geom_col(___)