Open3

ggplot2::after_stat()に関してまとめる

ピン留めされたアイテム
yuuyuu
  • 古いverのggplot2では..count..のように指定していたものが、after_stat()で利用できる
  • generated variables / computed variables と呼ぶ。
  • ggplot2内部で計算される変数という理解
  • 各関数stat_*のヘルプを見れば、何が計算されるかわかる
    • e.g.) geom_bar / stat_bar の場合は prop / count

下記のサイトに一覧としてまとまっている

下記のコードで変数を見ることが可能

p <- ggplot_build(ggplot(diamonds, aes(x = carat)) + geom_histogram())
#> names(p)
#> [1] "data"  "panel" "plot"

dplyr::glimpse(p$data[[1]])
yuuyuu

geom_bar()propを使ったサンプル

ggplot() +
 geom_bar(aes(y = after_stat(prop))
yuuyuu

geom_density()after_stat(scaled)を使う場合

ggplot(diamonds, aes(x = depth, fill = cut, colour = cut)) +
  geom_density(alpha = 0.1)

ggplot(diamonds, aes(x = depth, fill = cut, colour = cut)) +
  geom_density(aes(y = after_stat(scaled)), alpha = 0.1)