064 補足:集計済みデータの描画
補足:集計済みデータの描画~statオプション~
- 集計済みのデータを表示したい:
library(ggplot2) table <- data.frame( age_group = c("~20","21~40","41~60","61~80","81~100","100~"), yearly_admission = c(39,42,73,88,93,132) ) ggplot(table) + geom_bar(aes(age_group)) ggplot(table) + geom_bar(aes(age_group, yearly_admission), stat = "identity")
- このように、stat = "identity"というオプションをつけることで、geom_point等と同様の動作を設定できます。
ggplot(table) + geom_point(aes(age_group), stat="count")
- 通常のgeom_barのstat = "count" という設定を、手動で打ち消してあげることでこの動作になります。
ということは・・・
diamonds ggplot(diamonds) + geom_bar(aes(x = clarity)) ggplot(diamonds) + geom_point(aes(x = clarity)) #エラー! ggplot(diamonds) + geom_point(aes(x = clarity), stat = "count") #成功!
- このように、実は、変数×集計の組み合わせgeom_XXXの動作を変数×変数の組み合わせの動作にしたり、そのまた逆にしたりをstatオプションを設定することで実現できます。