exercise2
## 'data.frame': 2287 obs. of 6 variables:
## $ school: int 1 1 1 1 1 1 1 1 1 1 ...
## $ pupil : int 17001 17002 17003 17004 17005 17006 17007 17008 17009 17010 ...
## $ IQV : num 15 14.5 9.5 11 8 9.5 9.5 13 9.5 11 ...
## $ size : int 29 29 29 29 29 29 29 29 29 29 ...
## $ lang : int 46 45 33 46 20 30 30 57 36 36 ...
## $ arith : int 24 19 24 26 9 13 13 30 23 22 ...
## 將變量分等級
dta$level<-with(dta, factor(findInterval(size, c(-Inf,
quantile(size, probs=c(1/3, 2/3)), Inf)),
labels=c("Small","Medium","Large")))
dta$IQ<-with(dta, factor(findInterval(IQV, c(-Inf,
quantile(IQV, probs=c(1/3, 2/3)), Inf)),
labels=c("Low","Meddle","High")))
## 畫圖
library(ggplot2)
p0<-ggplot(data=dta, aes(lang, arith))+
labs(x="Language score", y="Arithmetic score")+
geom_point(shape=18, size=2)+ # change shape
# add regression line +SE, adjust size and color
stat_smooth(data=dta, formula=(y~x), method="lm", se=T, fill="#4D4D4D", colour="darkblue", size=0.5, alpha = 0.2)+
facet_wrap(.~level+IQ, labeller=labeller(.multi_line=F))+ # make two label at the same line
scale_x_continuous(breaks=seq(10, 50, 10))+ # adjust the tick label
scale_y_continuous(breaks=seq(5, 30, 5))
p0
