##load & see
data <- read.table("/Users/Tjlee/Desktop/weather/langMathDutch.txt", header = T)
head(data)## school pupil IQV size lang arith
## 1 1 17001 15.0 29 46 24
## 2 1 17002 14.5 29 45 19
## 3 1 17003 9.5 29 33 24
## 4 1 17004 11.0 29 46 26
## 5 1 17005 8.0 29 20 9
## 6 1 17006 9.5 29 30 13
## 0% 33.33333% 66.66667% 100%
## 4.0 11.0 12.5 18.0
classIQ <- with(data, cut(IQV, ordered=T, breaks=c(0, 11, 12.5, 18), labels=c("Low", "Middle","High")))
newdta <- cbind(data,classIQ)
head(newdta)## school pupil IQV size lang arith classIQ
## 1 1 17001 15.0 29 46 24 High
## 2 1 17002 14.5 29 45 19 High
## 3 1 17003 9.5 29 33 24 Low
## 4 1 17004 11.0 29 46 26 Low
## 5 1 17005 8.0 29 20 9 Low
## 6 1 17006 9.5 29 30 13 Low
## 0% 33.33333% 66.66667% 100%
## 5 20 27 37
classsize <- with(data, cut(size, ordered=T, breaks=c(0, 20, 27, 37), labels=c("Small", "Medium", "Large")))
ndata <- cbind(newdta,classsize)
head(ndata)## school pupil IQV size lang arith classIQ classsize
## 1 1 17001 15.0 29 46 24 High Large
## 2 1 17002 14.5 29 45 19 High Large
## 3 1 17003 9.5 29 33 24 Low Large
## 4 1 17004 11.0 29 46 26 Low Large
## 5 1 17005 8.0 29 20 9 Low Large
## 6 1 17006 9.5 29 30 13 Low Large
##cbind classsize&classIQ (use , split)
IQsize <- with(ndata,paste(classsize,classIQ,sep = ","))
head(IQsize)## [1] "Large,High" "Large,High" "Large,Low" "Large,Low" "Large,Low"
## [6] "Large,Low"
## school pupil IQV size lang arith classIQ classsize IQsize
## 1 1 17001 15.0 29 46 24 High Large Large,High
## 2 1 17002 14.5 29 45 19 High Large Large,High
## 3 1 17003 9.5 29 33 24 Low Large Large,Low
## 4 1 17004 11.0 29 46 26 Low Large Large,Low
## 5 1 17005 8.0 29 20 9 Low Large Large,Low
## 6 1 17006 9.5 29 30 13 Low Large Large,Low
##draw plot (lecture note p25)
library(gridExtra)
library(grid)
library(ggplot2)
library(lattice)
p1 <- ggplot(resultdata,
aes(lang, arith,color=IQsize)) +
geom_point(size=rel(.5),
alpha=.5) +
stat_smooth(method="lm",
formula=y ~ x,
se=F,
col='gray') +
facet_wrap(~ IQsize) +
labs(x="Language score", y="Arithmetic score") +
theme_bw()
p1p2 <- ggplot(resultdata,
aes(lang, arith)) +
geom_point(size=rel(1),#點#透明度
alpha=1) +
stat_smooth(method="lm", #線性
formula=y ~ x,
se=T, ##陰影部分
col='blue') +
facet_wrap(~ IQsize) + #依照IQsize排列
labs(x="Language score", y="Arithmetic score") +
theme_bw()
p2