히스토그램

ggplot2로 시작하는 데이터 시각화

Rick Scavetta

Founder, Scavetta Academy

자주 쓰는 그래프 유형

그래프 유형 가능한 Geom
산포도 points, jitter, abline, smooth, count
막대 그래프 histogram, bar, col, errorbar
선 그래프 line, path
ggplot2로 시작하는 데이터 시각화

히스토그램

ggplot(iris, aes(x = Sepal.Width)) + 
  geom_histogram()
  • 구간화된 값의 그래프
    • 즉, 통계 함수
`stat_bin()` using `bins = 30`.
Pick better value with `binwidth`.

ggplot2로 시작하는 데이터 시각화

기본값: 30개의 동일한 구간

ggplot(iris, aes(x = Sepal.Width)) + 
  geom_histogram()
  • 구간화된 값의 그래프
    • 즉, 통계 함수
# Default bin width:
diff(range(iris$Sepal.Width))/30
[1] 0.08

ggplot2로 시작하는 데이터 시각화

직관적이고 의미 있는 구간 너비

ggplot(iris, aes(x = Sepal.Width)) + 
  geom_histogram(binwidth = 0.1)
  • 항상 데이터에 의미 있는 빈 너비를 설정하세요.

  • 막대 사이 공백 없음.

ggplot2로 시작하는 데이터 시각화

눈금 위치 재배치

ggplot(iris, aes(x = Sepal.Width)) + 
  geom_histogram(binwidth = 0.1,
                 center = 0.05)
  • 항상 데이터에 의미 있는 빈 너비를 설정하세요.

  • 막대 사이 공백 없음.

  • X축 레이블은 막대 사이에 있습니다.

ggplot2로 시작하는 데이터 시각화

품종별 비교

ggplot(iris, aes(x = Sepal.Width, 
                 fill = Species)) + 
  geom_histogram(binwidth = .1, 
                 center = 0.05)

ggplot2로 시작하는 데이터 시각화

기본 position은 "stack"

ggplot(iris, aes(x = Sepal.Width,
                 fill = Species)) + 
  geom_histogram(binwidth = .1, 
                 center = 0.05,
                 position = "stack") 

ggplot2로 시작하는 데이터 시각화

position = "dodge"

ggplot(iris, aes(x = Sepal.Width, 
                 fill = Species)) + 
  geom_histogram(binwidth = .1, 
                 center = 0.05, 
                 position = "dodge")

ggplot2로 시작하는 데이터 시각화

position = "fill"

ggplot(iris, aes(x = Sepal.Width, 
                 fill = Species)) + 
  geom_histogram(binwidth = .1, 
                 center = 0.05, 
                 position = "fill")  

ggplot2로 시작하는 데이터 시각화

최종 슬라이드

ggplot2로 시작하는 데이터 시각화

Preparing Video For Download...