類別型 x 軸
在前面的圖中,你會發現腮腺炎(mumps)直到 1999 年才開始有回報,因此在那之前做比較沒有意義。
我們來篩選 1999 年(含)之後的資料,接著畫一張堆疊長條圖,觀察各地區不同疾病所佔的比例。
請修改資料處理的流程,把資料整理成需要的格式,然後建立並繪製堆疊長條圖!不必像上一題那樣調整長條的排序。看看是否出現讓你意外的模式?
本練習屬於課程
R 視覺化最佳實務
練習說明
- 將
who_disease資料篩選為只保留 1999 年及之後。 - 在
group_by()中加入欄位,讓摘要結果保留region資訊。 - 在對應美術屬性中填上
x = region、y = total_cases、fill = disease。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
disease_counts <- who_disease %>%
# Filter to on or later than 1999
filter(___) %>%
mutate(disease = ifelse(disease %in% c('measles', 'mumps'), disease, 'other')) %>%
group_by(disease, ___) %>% # Add region column to grouping
summarise(total_cases = sum(cases))
# Set aesthetics so disease is the stacking variable, region is the x-axis and counts are the y
ggplot(disease_counts, aes(___)) +
# Add a column geometry with the proper position value.
___(___)