Rstudio(6) 시각화 분석

hyukstory 혁스토리·2020년 8월 22일
0

Rstudio

목록 보기
8/16

< 6 > 시각화 분석

qplot() : 도수분포표 그래프

library(dplyr)
library(ggplot2)
mpg <- as.data.frame(ggplot2::mpg) ;mpg

qplot(mpg$std)

plot() : 막대그래프

gender <- c("M", "W", "W", "M", "M")
plot(gender) # 값이 character 이므로 에러
# factor로 변환하면 해결
gender <- factor(gender)
plot(gender)

new_gender<- factor(gender, levels=c('W','M')) # w m 순서 지정하는 법
plot(new_gender)

plot 그래프 타입 : types = c("p" , "l" , "o" , "b" , "c" , "s" , "S" , "h")

xx <- c(10 : 22)
yy <- xx^2

plot(xx, yy, type = "p", col = "red")
plot(xx, yy, type = "l", col = "red")
plot(xx, yy, type = "o", col = "red")
plot(xx, yy, type = "b", col = "red")
plot(xx, yy, type = "c", col = "red")
plot(xx, yy, type = "s", col = "red")
plot(xx, yy, type = "S", col = "red")
plot(xx, yy, type = "h", col = "red")


dev.new()
par(mfrow = c(2,4)) # 여러그래프 한 창에 보이기 ####
types = c("p" , "l" , "o" , "b" , "c" , "s" , "S" , "h")

for (i in 1:8) {
  plot(xx, yy, type = types[i], col = "orange")
}

직선 그리기

str(women)
plot(women)

wo_data <- lm(weight~height, women)
abline(wo_data, col="blue")

dev.new() : 새 창에서 그림 보는 함수

보려는 식과 같이 묶어 실행

head(mtcars)
car_cor <-cor(mtcars); car_cor
round(car_cor, 2)


install.packages("corrplot")
library(corrplot)
library(dplyr)

dev.new() 
corrplot(car_cor)

dev.new()
corrplot(car_cor, method = "number")

Q. 그래프 그리기 연습문제

(한국복지패널데이터)

성별에 따라 연봉이 다를까?

install.packages("foreign")
library(foreign) # SPSS 파일
library(dplyr) # 전처리
library(ggplot2) # 시각화
library(readxl) # 엑셀파일 불러오기

raw_welfare <- read.spss(file = "c:/TEMP/Rstudy/data/Koweps_hpc10_2015_beta1.sav", to.data.frame = T)

welfare <- raw_welfare # 복사본 만들기

# 변수값 수정
welfare <- rename(welfare,
                  sex = h10_g3,
                  birth = h10_g4,
                  marriage = h10_g10,
                  religion = h10_g11,
                  income = p1002_8aq1,
                  code_job = h10_eco9,
                  code_region = h10_reg7)


welfare$sex <- ifelse(welfare$sex == 1, 'male', 'female')
table(welfare$sex)

table(is.na(welfare$income)) # 결측치 확인

1. 성별 따른 월급 평균표 만들기

sex_income <- welfare %>% 
  filter(!is.na(income)) %>% 
  group_by(sex) %>% 
  summarise(mean_income = mean(income))
sex_income

ggplot(data = sex_income, aes(x = sex, y = mean_income)) + geom_col()

2. 나이에 따른 월급 평균표 만들기

#파생변수 나이 만들기
welfare$age <- (2020 - welfare$birth + 1)
summary(welfare$age)

View(welfare$age)

age_income <- welfare %>% 
  filter(!is.na(income)) %>% 
  group_by(age) %>% 
  summarise(mean_income = mean(income))

head(age_income)

ggplot(data = age_income, aes(x = age, y = mean_income)) + geom_col()
profile
문돌이의 고군분투 개발 공부

0개의 댓글