Ex1

library(lattice)
library(nlme)

dta <- Oxboys %>%
  as.data.frame() %>% 
  mutate(Subject = reorder(Subject, height, mean))

xyplot(dta$height~dta$age|dta$Subject,
       type = "b",
       layout = c(6,5),
       xlab = "Age (centered)",
       ylab = "Height (cm)")

Ex2

dta2 <- read.csv("income_tw.csv", header = TRUE)

ggplot(dta2, 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()

Ex3

library(tidyr)
dta3 <- read.csv("brainsize.txt", header = TRUE, sep = " ")

3-1

Are there gender differences in the three IQ scores?

dta3 %>% gather(Type, IQ, 3:5) %>% 
  ggplot(aes(Type, IQ, color = Gender))+
  stat_summary(fun.data = mean_se, geom = "pointrange", position = position_dodge(.2))

3-2

Is the relationship between height and weight gender dependent?

ggplot(dta3, aes(Height, Weight, color = Gender)) +
  geom_point() +
  stat_smooth(method = "lm", size = rel(1.1), se = F) +
  theme_bw()

3-3

Is the relationship between IQ and brainsize (as measured by MRIcount) gender dependent?

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

Delete this sentence and enter your text here.