ジオム外での統計

ggplot2 中級データ可視化

Rick Scavetta

Founder, Scavetta Academy

基本プロット

ggplot(iris, aes(x = Species, 
                 y = Sepal.Length)) +
  geom_jitter(width = 0.2)

ggplot2 中級データ可視化

統計量の計算

set.seed(123)
xx <- rnorm(100)
mean(xx)
[1] 0.09040591
mean(xx) + (sd(xx) * c(-1, 1))
[1] -0.822410  1.003222
ggplot2 中級データ可視化

統計量の計算

set.seed(123)
xx <- rnorm(100)

# Hmisc
library(Hmisc)
smean.sdl(xx, mult = 1)
       Mean       Lower       Upper 
 0.09040591 -0.82240997  1.00322179
# ggplot2
mean_sdl(xx, mult = 1)
           y     ymin     ymax
1 0.09040591 -0.82241 1.003222
ggplot2 中級データ可視化

stat_summary()

ggplot(iris, aes(x = Species, 
                 y = Sepal.Length)) +
 stat_summary(fun.data = mean_sdl, 
                fun.args = list(mult = 1))

・既定で geom_pointrange() を使用

ggplot2 中級データ可視化

stat_summary()

ggplot(iris, aes(x = Species, 
                 y = Sepal.Length)) +
  stat_summary(fun = mean,
               geom = "point") +
  stat_summary(fun.data = mean_sdl,
               fun.args = list(mult = 1),
               geom = "errorbar",
               width = 0.1)

ggplot2 中級データ可視化

推奨しません

ggplot2 中級データ可視化

95% 信頼区間

ERR <- qt(0.975, length(xx) - 1) * (sd(xx) / sqrt(length(xx)))
mean(xx)
0.09040591
mean(xx) + (ERR * c(-1, 1)) # 95% CI
-0.09071657  0.27152838
mean_cl_normal(xx)
           y        ymin      ymax
0.09040591 -0.09071657 0.2715284
ggplot2 中級データ可視化

その他の stat_ 関数

stat_ 説明
stat_summary() 各 x の y を要約します。
stat_function() x の関数から y を計算します。
stat_qq() Q-Q プロット用の計算を行います。
ggplot2 中級データ可視化

MASS::mammals

ggplot2 中級データ可視化

正規分布

mam.new <- data.frame(body = log10(mammals$body))

ggplot(mam.new, aes(x = body)) + 
  geom_histogram(aes( y = ..density..)) +
  geom_rug() +
  stat_function(fun = dnorm, color = "red", 
                args = list(mean = mean(mam.new$body),
                            sd = sd(mam.new$body)))

ggplot2 中級データ可視化

Q-Q プロット

ggplot(mam.new, aes(sample = body)) + 
  stat_qq() +
  geom_qq_line(col = "red")

ggplot2 中級データ可視化

あなたの番です

ggplot2 中級データ可視化

Preparing Video For Download...