Question 1

library(nlme)
## Warning: package 'nlme' was built under R version 3.4.4
## 
## Attaching package: 'nlme'
## The following object is masked from 'package:dplyr':
## 
##     collapse
dta<-Oxboys
head(dta)
## Grouped Data: height ~ age | Subject
##   Subject     age height Occasion
## 1       1 -1.0000  140.5        1
## 2       1 -0.7479  143.4        2
## 3       1 -0.4630  144.8        3
## 4       1 -0.1643  147.1        4
## 5       1 -0.0027  147.7        5
## 6       1  0.2466  150.2        6
str(dta)
## Classes 'nfnGroupedData', 'nfGroupedData', 'groupedData' and 'data.frame':   234 obs. of  4 variables:
##  $ Subject : Ord.factor w/ 26 levels "10"<"26"<"25"<..: 13 13 13 13 13 13 13 13 13 5 ...
##  $ age     : num  -1 -0.7479 -0.463 -0.1643 -0.0027 ...
##  $ height  : num  140 143 145 147 148 ...
##  $ Occasion: Ord.factor w/ 9 levels "1"<"2"<"3"<"4"<..: 1 2 3 4 5 6 7 8 9 1 ...
##  - attr(*, "formula")=Class 'formula'  language height ~ age | Subject
##   .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
##  - attr(*, "labels")=List of 2
##   ..$ y: chr "Height"
##   ..$ x: chr "Centered age"
##  - attr(*, "units")=List of 1
##   ..$ y: chr "(cm)"
##  - attr(*, "FUN")=function (x)  
##   ..- attr(*, "source")= chr "function (x) max(x, na.rm = TRUE)"
##  - attr(*, "order.groups")= logi TRUE
library(lattice)
xyplot(dta$height~dta$age | Subject,data=dta,grid = TRUE,
       type = c("p", "smooth"), 
       col.line = "darkorange", 
       xlab = "Age (centered)", ylab = "Height (cm)",
       lwd =1)

Question 2

dta2<-read.table("dta2.txt", header=T,sep = ",")
str(dta2)
## 'data.frame':    41 obs. of  2 variables:
##  $ Income: Factor w/ 41 levels "1,000,000 to 1,069,999",..: 7 8 9 11 12 13 14 15 16 17 ...
##  $ Count : int  807160 301650 313992 329290 369583 452671 495387 517779 557786 584497 ...
dta2$Income<-factor(dta2$Income,levels =dta2$Income[order(dta2$Count)] )
ggplot(dta2, aes(Count/1000, Income)) +
  geom_point() +
  geom_segment(aes(x = 0, xend = Count/1000, yend=Income)) +
  labs(x= "Number of persons(x1,000)", y = "Income", title = "Distribution of personal disposable income in Taiwan in 2015")

Question 3_1

library(tidyr)
## 
## Attaching package: 'tidyr'
## The following object is masked from 'package:Matrix':
## 
##     expand
dta3<-read.table("dta3.txt",header=T) %>% gather(IQs, score, 3:5)
ggplot(dta3, aes(IQs, score, color = Gender)) +
  stat_summary(fun.data = mean_se, geom = "pointrange", position = position_dodge(0.2))

Question 3_2

ggplot(dta3, aes(Height, Weight, color = Gender)) +
  geom_point() +
  stat_smooth(method = "lm", size = rel(1.1), se = F) +
  theme_bw()
## Warning: Removed 6 rows containing non-finite values (stat_smooth).
## Warning: Removed 6 rows containing missing values (geom_point).

Question 3_3

ggplot(dta3, aes(score, MRICount, color = Gender)) +
  geom_point() +
  stat_smooth(method = "lm", size = rel(1.1), se = F) +
  facet_wrap(~IQs) +
  theme_bw()

The End