pacman::p_load(nlme,magrittr,tidyverse,ggplot2)

EX01

library(lattice)
dta <- Oxboys 
xyplot(x=height~age | Subject,data=dta,layout = c(6,5),
panel=function(x,y){
panel.xyplot(x,y) 
panel.lmline(x,y)
}
)

EX02

dta02 <- read.csv("income_tw.csv", header = T) 
ggplot(dta02, aes(reorder(Income, Count), Count))+
  geom_point()+
  geom_segment(aes(xend = reorder(Income, Count),y = 0, yend = Count))+
  labs(x = "Income (reorder by Count)")+
  coord_flip() 

EX03

dta03 <- read.table("brainsize.txt", header = TRUE)

3-1

dta03 %>%
gather(IQtype, IQvalue, 3:5) %>% 
ggplot(aes(IQtype, IQvalue, color = Gender)) +
stat_summary(fun.data = mean_se, geom = "pointrange",
size = rel(1.1), position = position_dodge(0.3))

3-2

dta03 %>%
ggplot(aes(Height, Weight)) +
geom_point(aes(color = Gender)) +
stat_smooth(aes(color = Gender), method = "lm", se = F) 
## Warning: Removed 2 rows containing non-finite values (stat_smooth).
## Warning: Removed 2 rows containing missing values (geom_point).

3-3

dta03 %>%
gather(IQtype, IQ, 3:5) %>% 
ggplot(aes(IQ, MRICount, color = Gender, group= Gender)) +
geom_point() +
stat_smooth(method = "lm", se = F) +
facet_grid(. ~ IQtype) +
labs(y = "Brain Size")