# Chapter 6 : 데이터 시각화_ggplot2 패키지

1. gglot 패키지 설치 및 로드하기

#install.packages('ggplot2')
library(ggplot2)

2. 그래프에 객체 추가하기

1) 사선 그리기 : geom_abline()

geom_abline(intercept = 절편, slope = 기울기)

  • ggplot2 패키지 로드 및 economics 데이터 세트 구조 확인인
library(ggplot2)
str(economics)
## spc_tbl_ [574 × 6] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ date    : Date[1:574], format: "1967-07-01" "1967-08-01" ...
##  $ pce     : num [1:574] 507 510 516 512 517 ...
##  $ pop     : num [1:574] 198712 198911 199113 199311 199498 ...
##  $ psavert : num [1:574] 12.6 12.6 11.9 12.9 12.8 11.8 11.7 12.3 11.7 12.3 ...
##  $ uempmed : num [1:574] 4.5 4.7 4.6 4.9 4.7 4.8 5.1 4.5 4.1 4.6 ...
##  $ unemploy: num [1:574] 2944 2945 2958 3143 3066 ...
  • 그래프에 사선 그리기
ggplot(economics, aes(x = date, y = psavert)) + geom_line() + geom_abline(intercept = 12.18671, slope = -0.0005444)

2) 평행선 그리기 : geom_hline()

geom_hline(yintercept = y절편)

  • 그래프에 평행선 그리기
ggplot(economics, aes(x = date, y = psavert)) + geom_line() + geom_hline(yintercept = mean(economics$psavert))

3) 수직선 그리기 : geom_vline()

geom_vline(xintercept = x 절편)

  • 그래프에 수직선 그리기
library(dplyr)
## 
## 다음의 패키지를 부착합니다: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
x_inter <- filter(economics, psavert == min(economics$psavert))$date

ggplot(economics, aes(x = date, y = psavert)) + geom_line() + geom_vline(xintercept = x_inter) 

4) 레이블 입력하기 : geom_text()

geom_text(aes(label = 레이블, vjust = 세로 위치, hjust = 가로위치))

  • 그래프에 텍스트 입력하기
ggplot(airquality, aes(x = Day, y = Temp)) + geom_point() + geom_text(aes(label = Temp, vjust = 0, hjust = 0))

5) 도형 및 화살표 넣기 : annotate()

annotate("모양", xmin = x축 시작, xmax = x축 끝, ymin = y축 시작, ymax = y축 끝)

  • 그래프에 사각형 그리기
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + annotate('rect', xmin = 3, xmax = 4, ymin = 12, ymax = 21, alpha = 0.5, fill = 'skyblue')

  • 그래프에 화살표 그리기
ggplot(mtcars, aes(x = wt, y= mpg)) + geom_point() + annotate('rect', xmin = 3, xmax = 4, ymin = 12, ymax = 21, alpha = 0.5, fill = 'skyblue') + annotate('segment', x=2.5, xend = 3.7, y = 10, yend =17, color = 'red', arrow = arrow())

  • 그래프에 도형과 텍스트를 함께 표시하기
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + annotate('rect', xmin = 3, xmax = 4, ymin = 12, ymax = 21, alpha = 0.5, fill = 'skyblue') + 
  annotate("segment", x = 2.5,xend = 3.7, y = 10, yend = 17, color = 'red', arrow = arrow()) +
  annotate('text', x = 2.5, y = 10, label = 'point')

3. 그래프와 축에 제목 추가하고 디자인 테마 적용하기

1) 그래프 제목 및 축 제목 추가하기 : labs

labs(x = 'x축명', y = 'y축명', title = '그래프 제목')

  • 그래프와 축 제목 추가하기
ggplot(mtcars, aes(x = gear)) + geom_bar() + labs(x = '기여수', y = '자동차수', title = '변속기 기어별 자동차수')

2) 디자인 테마 적용하기 : theme()

* theme_gray() : 회색 바탕과 흰 선
* theme_bw() : 흰 바탕과 회색 선
* theme_linedraw() : 흰 바탕과 가늘고 검은 선
* theme_light() : 밝은 회색 바탕
* theme_dark() : 어두운 바탕
* theme_minimal() : 단순한 배경
* theme_classic() : 눈금과 안내선이 없는 기본 바탕
* theme_void() : 가장 간결한 바탕

4. 절편과 기울기 구하기 : 회귀분석

lm (종속변수 ~ 독립변수, data = 데이터 세트)

library(readxl)
exdata1 <- read_excel("C:/Users/DaBin/Desktop/혼자 공부하는 R 데이터 분석/Sample1.xlsx")

1) 두 변수 간 상관관계 파악하기 : cor.test()

상관분석 : cor.test(테이블명$변수명1, 테이블명$변수명2)

  • 상관분석하여 두 변수 간의 상관관계 확인하기
cor.test(exdata1$Y20_CNT, exdata1$Y21_CNT)
## 
##  Pearson's product-moment correlation
## 
## data:  exdata1$Y20_CNT and exdata1$Y21_CNT
## t = 4.9343, df = 18, p-value = 0.000107
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.4751688 0.8990895
## sample estimates:
##       cor 
## 0.7582507

2) 절편과 기울기 구하기 : lm()

  • 회귀분석하여 절편과 기울기 구하기
reg_result <- lm(Y21_CNT ~ Y20_CNT, data = exdata1)
reg_result
## 
## Call:
## lm(formula = Y21_CNT ~ Y20_CNT, data = exdata1)
## 
## Coefficients:
## (Intercept)      Y20_CNT  
##      0.7104       0.7864

확인문제

#1번 : 양의 상관관계계

data(iris)
cor.test(iris$Sepal.Length, iris$Petal.Length)
## 
##  Pearson's product-moment correlation
## 
## data:  iris$Sepal.Length and iris$Petal.Length
## t = 21.646, df = 148, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.8270363 0.9055080
## sample estimates:
##       cor 
## 0.8717538
#2번

iris_result <- lm(Petal.Length ~ Sepal.Length, data = iris)
iris_result
## 
## Call:
## lm(formula = Petal.Length ~ Sepal.Length, data = iris)
## 
## Coefficients:
##  (Intercept)  Sepal.Length  
##       -7.101         1.858
#3번

library(ggplot2)
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) +geom_line() + geom_abline(intercept = -7.101, slope = 1.858)