# 데이터 로드
df <- read.csv('r-ggagi-data/example_studentlist.csv')
str(df)
# 변수 1개
plot(df$age) # 첫 번째 행부터 마지막 행까지 산점도로 표시
# 변수 2개 - 상관 관계
plot(df$height, df$weight)
# 변수 2개 - 상관 관계 - 종속변수(y) ~ 독립변수(x)
plot(df$height ~ df$weight)
# 수치형 변수, 명목형 변수 1 남자, 2 여자
plot(df$height, df$sex)
# 명목 형 변수, 수치형 변수 1 남자, 2 여자
plot(df$sex, df$height)
# 수치형 변수, 명목형 변수 1 남자, 2 여자
plot(df$height~ df$sex)
# 명목 형 변수~ 수치형 변수 1 남자, 2 여자
plot(df$sex~ df$height)
# 데이터 프레임 넣기
df2 <- data.frame(df$height, df$weight)
plot(df2)
# 변수 하나하나가 차원임 3차원은 못만들어서 관련된 2차원 그래프 여러개 작성
df3 <- data.frame(df2, df$age)
plot(df3)
# 데이터 프레임 넣기
plot(df)
# 성별 별도 표시
plot(df$weight~df$height, pch=as.integer(df$sex))
# 성별 별도 표시 - 레전드 추가
plot(df$weight~df$height, pch = as.integer(df$sex))
legend('topleft', c('man', 'woman'), pch=df$sex)
# 성별에 따른 키와 몸무게
coplot(df$weight ~ df$height | df$sex)
# 저수준 그래프 함수
# 제목 달기 - ann = F 모든 라벨 삭제
plot(df$weight ~ df$height, ann=F)
title(main = '몸무게와 키의 상관관계')
title(ylab = '몸무게')
title(xlab = '키')
grid() # 격자 추가
abline(v=mean(df$height), h=mean(df$weight), col='red')
blood_type <- table(df$bloodtype)
blood_type
barplot(blood_type)
title(main = '혈액형 빈도수')
title(xlab = '혈액형')
title(ylab = '빈도수')
# 그룹별 평균
height_blood <- tapply(df$height, df$bloodtype, mean)
height_blood
barplot(height_blood, ylim = c(0, 200))
# 빈도수는 plot() 바로 그릴 수 있음
plot(df$bloodtype)
boxplot(df$height)
# levels 별
boxplot(df$height ~ df$bloodtype)
subset(df, subset=(df$bloodtype == 'O'))
hist(df$height)
# 막대수 조정
hist(df$height, breaks = 10)
# 상대도수밀도 prob = T, 곡선 추가 lines
# 바의 면적이 상대도수
hist(df$height, breaks=10, prob=T)
lines(density(df$height))
# 계급 7간격 만들기
break_point <- seq(min(df$height), max(df$height)+7, by=7)
hist(df$height, breaks = break_point)
# 계급 수동 설정 => 상대밀도함수(Density)로 바뀜
diff_point <- c(min(df$height), 165, 170, 180, 185)
hist(df$height, breaks=diff_point)
par(mfrow = c(2,3))
plot(df$weight, df$height)
plot(df$sex, df$height)
barplot(table(df$bloodtype))
boxplot(df$height)
boxplot(df$height ~ df$bloodtype)
hist(df$height, breaks = 10)
# 다시 1개씩 그리기
par(mfrow = c(1,1))
plot(df$weight~df$height + df$age + df$grade + df$absence + df$sex)
# 시계열 변수 임의로 만들기
runif(30) # 난수 발생
round(runif(30)*100) # 소수점 처리
TS1 <- c(round(runif(30)*100))
TS1
TS2 <- c(round(runif(30)*100))
TS2
# 자료 정렬
TS1 <- sort(TS1, decreasing=F)
TS2 <- sort(TS2, decreasing=F)
TS1
TS2
plot(TS1, type='l')
plot(TS1, type='l')
lines(TS2, lty='dashed', col='red')
x1 <- seq(1,100,1)
y1 <- dbinom(x1, 100, 0.25) # dbinom(v,n,p) B(n,p)이항 분포 함수 값 만들기
head(y1)
x2 <- seq(1,50,1)
y2 <- dbinom(x2, 50, 0.5) # dbinom(v,n,p) B(n,p)이항 분포 함수 값 만들기
head(y2)
plot(x1, y1, type='h', ylim=c(0, 0.15), xlim=c(0, 60))
lines(x2, y2, col='red')
library('ggplot2')
library('ggthemes')
g1 <- ggplot(data=diamonds, aes(x=carat, y=price, colour=clarity))
g2 <- geom_point()
g3 <- theme_wsj()
g1+g2+g3
# 테마만 바꾸기
g1 + g2 + theme_bw()
# 데이터 로드
df <- read.csv('r-ggagi-data/example_studentlist.csv')
g1 <- ggplot(df, aes(x=height, y=weight, colour=bloodtype))
g1 + geom_point()
g1 + geom_line()
g1 + geom_point() + geom_line()
g1 + geom_line(aes(colour=sex)) + geom_point(size=10)
g1 + geom_point(size=10) + geom_line(size=1) + facet_grid(.~sex) # 성별 독립변수
g1 + geom_point(size=10) + geom_line(size=1) + facet_grid(sex~.) # 성별 종속변수
# y축 범위(scale) 각각 맞게 처리
g1 + geom_point(size=10) + geom_line(size=1) + facet_grid(sex~., scale='free') # 성별 종속변수
# y축 scale 적용 안됨
g1 + geom_point(size=10) + geom_line(size=1) + facet_grid(.~sex, scale='free') # 성별 독립변수
# y축 scale 적용
g1 + geom_point(size=10) + geom_line(size=1) + facet_wrap(~sex, scale='free') # 성별 독립변수
# facet_grid() - 명목형 변수들의 level 별 그래프를 보여주는 목적
g <- ggplot(mpg, aes(displ, hwy)) # 배기량, 고속도로연비 산점도
g + geom_point()
# 차량 종류에 따른 배기량, 고속도로연비 보기(그룹)
g + geom_point() + facet_grid(.~class)
# 종속변수 추가
g + geom_point(alpha=.3) + facet_grid(cyl~class, scale='free')
# 종속변수 추가 - 각각의 그래프를 모아서 보기 위함
g + geom_point(alpha=.3) + facet_wrap(cyl~class, scale='free')
ggplot(df, aes(x=bloodtype)) + geom_bar()
# level 별 색 넣기 - fill
ggplot(df, aes(x=bloodtype, fill=sex)) + geom_bar()
# Level 별 색 넣고 각각 바로 표시 - position
ggplot(df, aes(x=bloodtype, fill=sex)) + geom_bar(position = 'dodge')
# Level 별 색 넣고 가각 바로 표시 - position
ggplot(df, aes(x=bloodtype, fill=sex)) + geom_bar(position = 'identity') # 누적없이 겹치기
# Level 별 색 넣고 가각 바로 표시 - position
ggplot(df, aes(x=bloodtype, fill=sex)) + geom_bar(position = 'fill') # 비율로 표시
# 막대 넓이 바꾸기 - width
ggplot(df, aes(x=bloodtype, fill=sex)) + geom_bar(position = 'dodge', width=0.5)
# 도수 값 말고 계산된 값 사용하기 - 혈액형별 키 평균
height_mean <- tapply(df$height, df$bloodtype, mean)
height_mean
# 변수명 주기
df2 <- data.frame(height_mean)
df2$bloodtype <- rownames(df2)
rownames(df2) <- NULL
df2
# stat = 'identity' 값 그대로, 생략시 stat = 'bin' 빈도수 표시
ggplot(df2, aes(x=bloodtype, y=height_mean, fill=bloodtype))+
geom_bar(stat='identity') +
scale_fill_brewer() # 색 칠할 때 도와주는 함수 scale_fill_brewer(palette = '색상')으로 변경가능
g1 <- ggplot(diamonds, aes(x=carat))
# y축 계급 빈도수
g1 + geom_histogram(binwidth = 0.1, fill = 'orange')
# 예약어 - ..count.. 도수 default
g1 + geom_histogram(aes(y=..count..), binwidth = 0.1, fill='orange')
# 예약어 - ..ncount.. 표준화된 도수
g1 + geom_histogram(aes(y=..ncount..), binwidth = 0.1, fill='orange')
# 예약어 - ..density.. 밀도
g1 + geom_histogram(aes(y=..density..), binwidth = 0.1, fill='orange')
# 예약어 - ..ndensity.. 표준화된 밀도
g1 + geom_histogram(aes(y=..ndensity..), binwidth = 0.1, fill='orange')
# 그룹별로 그리기 - facet_grid
g1 + geom_histogram(binwidth = 0.1, fill='orange') +
facet_grid(color~.) # color 변수 Level 별로
# 그룹별로 그리기 - 축변경 facet_grid - scales='free'
g1 + geom_histogram(binwidth = 0.1, fill='orange') +
facet_grid(color~., scales='free') # color 변수 Level 별로
# level 별로 겹쳐 보이기
g1 + geom_histogram(aes(fill=color), binwidth = 0.1, alpha = 0.5)
# 데이터 로드
df <- read.csv('r-ggagi-data/example_studentlist.csv')
head(df)
g1 <- ggplot(df, aes(x=weight, y=height))
g1 + geom_point()
# level 별 색상 주기
g1 + geom_point(aes(colour=sex), size=7)
# 점 모양 바꾸기 1
g1 + geom_point(aes(colour=sex, shape=sex), size=7)
# 점 모양 바꾸기 2
g1 + geom_point(aes(colour=sex, shape=bloodtype), size=7)
# colour 에 연속형 변수 넣기
g1 + geom_point(aes(colour=height, shape=sex), size=7)
# size 에 연속형 변수 넣기
g1 + geom_point(aes(size=height, shape=sex), colour='orange')
# 산점도로 회귀분석의 그리기
g1 + geom_point(aes(colour=sex), size=7) +
geom_smooth(method = 'lm')
# 점마다 이름 넣기 - geom_text()
g1 + geom_point(aes(colour=sex), size=7) +
geom_text(aes(label=name))
# 점마다 이름 넣기 - 위치조절 vjust, 글자색 colour
g1 + geom_point(aes(colour=sex), size=7) +
geom_text(aes(label=name), vjust=-1.5, colour='grey35')