始める無料で始める

基本の積み上げ棒グラフ

前の演習で円グラフとワッフルチャートを作成したときは、すべての年をまとめて見ていました。

ここでは、これらのパターンを時間の経過とともに見ていきます。そのために、観測年をx軸にした積み上げ棒グラフを作成します。先ほど円グラフで行ったのと同様に、データは measles、mumps、other の3種類に簡略化します。

この見せ方にすると、データに何か気になる点はありますか?

この演習はコースの一部です

R による可視化ベストプラクティス

コースを見る

演習の手順

  • aes() の指定を変更して、x軸に year をマッピングしてください。
  • 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(___)
コードを編集して実行