#install.packages('ggplot2')
library(ggplot2)
geom_abline()geom_abline(intercept = 절편, slope = 기울기)
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)
geom_hline()geom_hline(yintercept = y절편)
ggplot(economics, aes(x = date, y = psavert)) + geom_line() + geom_hline(yintercept = mean(economics$psavert))
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)
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))
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')
labslabs(x = 'x축명', y = 'y축명', title = '그래프 제목')
ggplot(mtcars, aes(x = gear)) + geom_bar() + labs(x = '기여수', y = '자동차수', title = '변속기 기어별 자동차수')
theme()* theme_gray() : 회색 바탕과 흰 선
* theme_bw() : 흰 바탕과 회색 선
* theme_linedraw() : 흰 바탕과 가늘고 검은 선
* theme_light() : 밝은 회색 바탕
* theme_dark() : 어두운 바탕
* theme_minimal() : 단순한 배경
* theme_classic() : 눈금과 안내선이 없는 기본 바탕
* theme_void() : 가장 간결한 바탕
lm (종속변수 ~ 독립변수, data = 데이터 세트)
library(readxl)
exdata1 <- read_excel("C:/Users/DaBin/Desktop/혼자 공부하는 R 데이터 분석/Sample1.xlsx")
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
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)