Scatterplot을 그리고 싶을 때 사용한다.
이 데이터를 사용해서 보면 :
set.seed(955)
# 증가하는 형태의 데이터를 생성
dat <- data.frame(cond = rep(c("A", "B"), each = 10), xvar = 1:20 + rnorm(20,
sd = 3), yvar = 1:20 + rnorm(20, sd = 3))
# cond xvar yvar A -4.252354091 3.473157275 A 1.702317971 0.005939612 ...
# B 17.793359218 19.718587761 B 19.319909163 19.647899863
library(ggplot2)
ggplot(dat, aes(x=xvar, y=yvar)) +
geom_point(shape=1) # ○(shape=1)을 점의 모양으로 사용
ggplot(dat, aes(x=xvar, y=yvar)) +
geom_point(shape=1) + # ○(shape=1)을 점의 모양으로 사용
geom_smooth(method=lm) # 회귀직선을 함께 보여줌
# lm의 초기값은 회귀직선과 95% 신뢰구간이 함께 보여짐
ggplot(dat, aes(x=xvar, y=yvar)) +
geom_point(shape=1) + # ○(shape=1)을 점의 모양으로 사용
geom_smooth(method=lm, # 회귀직선을 함께 보여줌
se=FALSE) # 신뢰구간을 보여주지 않음
ggplot(dat, aes(x=xvar, y=yvar)) +
geom_point(shape=1) + # ○(shape=1)을 점의 모양으로 사용
geom_smooth() # 회귀곡선과 신뢰구간을 보여줌
# cond변수에 따라 점의 색상 설정
ggplot(dat, aes(x=xvar, y=yvar, color=cond)) + geom_point(shape=1)
# 앞 그래프와 같지만, 색상이 다르고 회귀직선을 추가하도록 설정
ggplot(dat, aes(x=xvar, y=yvar, color=cond)) + geom_point(shape=1) +
scale_colour_hue(l=50) + # 보통보다 좀 더 어두운 색상을 사용(l=50)
geom_smooth(method=lm, # 회귀직선을 함께 보여줌
se=FALSE) # 신뢰구간을 보여주지 않음
# 데이터의 영역을 넘어 회귀직선 확장하도록 설정
ggplot(dat, aes(x=xvar, y=yvar, color=cond)) + geom_point(shape=1) +
scale_colour_hue(l=50) + # 보통보다 좀 더 어두운 색상을 사용(l=50)
geom_smooth(method=lm, # 회귀직선을 함께 보여줌
se=FALSE, # 신뢰구간을 보여주지 않음
fullrange=T) # 회귀직선을 확장하도록 설정
# cond변수에 따라 점의 색상 설정
ggplot(dat, aes(x=xvar, y=yvar, shape=cond)) + geom_point()
# 앞 그래프와 같지만, 점의 형태가 다르게 설정
ggplot(dat, aes(x=xvar, y=yvar, shape=cond)) + geom_point() +
scale_shape_manual(values=c(1,2)) # ○와 △을 점의 모양으로 사용
색상과 형태에 관한 좀 더 많은 정보를 원하면 Colors(ggplot2)/)와 Shapes and line types 에서 찾을 수 있다.
너무 많은 데이터를 가지고 있거나 데이터의 척도가 이산이라면, data points가 overlap된다. 따라서 많은 점들이 같은 지역에 점이 찍히게 되어 이것을 확인하는 것은 불가능하다.
# Round xvar and yvar to the nearest 5
dat$xrnd <- round(dat$xvar/5)*5
dat$yrnd <- round(dat$yvar/5)*5
# 투명도를 부분적으로 설정을 해서, 한 번 overplootting될때마다 불투명도가 1/4씩 증가하도록 설정
# 좀 더 heavy한 overplotting이라면 불투명도를 좀 더 작게 설정
ggplot(dat, aes(x=xrnd, y=yrnd)) +
geom_point(shape=19, # ●(shape=19)을 점의 모양으로 사용
alpha=1/4) # 불투명도를 1/4로 사용
# Jitter the points
# Jitter range is 1 on the x-axis, .5 on the y-axis
ggplot(dat, aes(x=xrnd, y=yrnd)) +
geom_point(shape=1, # ○(shape=1)을 점의 모양으로 사용
position=position_jitter(width=1,height=.5))
Hankuk University of Foreign Studies. Dept of Statistics. Daewoo Choi Lab. Yeeseul Han.
한국외국어대학교 통계학과 최대우 연구실 한이슬
e-mail : han.lolove17@gmail.com