tidyr、ggplot、lattice

畫圖

KY

2020-04-22

##使用最近會用到畫圖語法
library(lattice)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
library(ggplot2)

##查看資料型式
dta<- read.table("C:/tmp/langMathDutch.txt", h = T)
head(dta)
##   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
##tidyr語法切割三等份

dta <- dta %>% mutate(sizef=cut(size, 
                      breaks=quantile(size, 
                                      probs=c(0, .33, .66, 1)),
                      label=c("Q1", "Q2", "Q3"), 
                      ordered=T, 
                      include.lowest=T))
dta <- dta %>% mutate(IQV_f=cut(IQV, 
                      breaks=quantile(IQV, 
                                      probs=c(0, .33,.66, 1)),
                      label=c("IQ1", "IQ2", "IQ3"), 
                      ordered=T, 
                      include.lowest=T))
##查看後是也分好三等份了
head(dta)
##   school pupil  IQV size lang arith sizef IQV_f
## 1      1 17001 15.0   29   46    24    Q3   IQ3
## 2      1 17002 14.5   29   45    19    Q3   IQ3
## 3      1 17003  9.5   29   33    24    Q3   IQ1
## 4      1 17004 11.0   29   46    26    Q3   IQ1
## 5      1 17005  8.0   29   20     9    Q3   IQ1
## 6      1 17006  9.5   29   30    13    Q3   IQ1
##和想要的差了一些距離
p1 <- ggplot(dta, 
             aes(size,IQV)) +
  geom_point(size=rel(.5), 
             alpha=.5) +
  stat_smooth(method="lm", 
              formula=y ~ x,
              se=F,
              col='gray',
              size=rel(.5)) +
  facet_grid(sizef ~ IQV_f) +
  theme_bw() +
  theme(axis.text.x=element_text(angle=60, 
                                 hjust=1))
p1

##改用Lattice試試
xyplot(lang~IQV|size,data=dta,
       type=c("p","g","r"),layout=c(3,1),
       xlab="lang1表現",
       ylab="IQV高低")

##資料經過切割和新增後,分組有出來,但畫圖語法需要再修正