カテゴリ型のx軸
前のチャートから、流行性耳下腺炎(mumps)は1999年までは報告が始まっていなかったことが分かりました。つまり、それ以前の比較は意味がありません。
そこで、1999年以降に報告された症例だけにデータを絞り込み、地域ごとの疾患の比率を積み上げ棒グラフで見てみましょう。
データ操作のパイプラインを修正して、必要な形に整えてから、積み上げ棒グラフを作成してプロットしてください。前の演習のように棒の並び順は気にしなくて大丈夫です。意外なパターンは見つかりますか?
この演習はコースの一部です
R による可視化ベストプラクティス
演習の手順
who_diseaseデータを1999年以降の年だけにフィルターします。- サマリーに
region情報を残すために、group_by()にregionを追加します。 - 美的属性(aesthetics)に
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.
___(___)