R/Exploratory data analysis

ggplot2:: geom_smooth()

Abokadoh 2023. 4. 6. 01:14

ggplot의 geom_smooth()에 대해서 알아보자.

 

geom_smooth()는 geom()함수를 사용한 시각화에서 데이터의 양이 많아서 overplotting이 발생할 때 패턴을 볼 수 있도록 smooth한 line을 제공해 데이터의 특성이나 분포를 보다 더 잘 이해할 수 있도록 돕는다.

 

코드를 통해 이게 무슨 말인지 이해해보자.

 

1. geom_smooth()

ggplot(mpg, aes(displ, hwy)) +
  geom_point()
  geom_smooth()

 

geom_smooth()의 method argument에 대해서 알아보자. 

 

ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  geom_smooth(method = 'lm')

여기서 method 인자를 따로 선택하지 않았을 때와는 다른 추세선이 나타난다. 여기서 lm은 linear model로 그래프에 포함된 직선은 선형회귀모델로 만든 선형 회귀선이다.

추세선 근처에 그림자처럼 나타나 있는 것은 오차 범위인데 se argument를 사용해 표기하지 않는 방법도 있다

ggplot(mpg, aes(displ, hwy)) +
  geom_point()
  geom_smooth(method = 'lm', se = FALSE)