지오메트릭 외부의 통계

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() 분위수–분위수 도표 계산을 수행합니다.
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로 하는 중급 데이터 시각화

QQ 플롯

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

ggplot2로 하는 중급 데이터 시각화

이제 실습!

ggplot2로 하는 중급 데이터 시각화

Preparing Video For Download...