setwd("/Users/tayloryen/Desktop/大學/成大課業/大四下/資料管理/0507/HW")
library(tidyr)
## 
## Attaching package: 'tidyr'
## The following object is masked from 'package:Matrix':
## 
##     expand

Question 2

dta2<-read.table("dta2.txt",header=T,sep=",")
##成績和年齡在性別上的差異
dta2 %>% gather(subject,score,8:9) %>%
  ggplot(.,aes(year,score,color=sex)) +
  stat_summary(fun.data = mean_se , geom = "pointrange",
               position = position_dodge(0.3)) + 
  facet_grid(.~subject)+
  theme_light()

##性別和種族在閱讀和數學分數的分佈
dta2 %>% ggplot()+ 
  geom_smooth(mapping=aes(math,read,color=sex))  +
  theme_light() + 
  facet_grid(.~race)
## `geom_smooth()` using method = 'loess'

Question 3

dta3<-read.table("dta3.txt",header=T,sep=",")%>%mutate(grp = if_else(Age >= 21, "Yes", "No"))
## Warning: package 'bindrcpp' was built under R version 3.4.4
xyplot(Alcohol ~ Age, group = grp,
       data = dta3, type = c("g", "r", "p"), auto.key = list(column = 2),
       xlab = "Age (year)", ylab = "Mortality rate from alcohol abuse (per 100,000)")

##講義上面的圖還在想要如何繪製。
##單就兩張圖來比較的話,右邊那張圖表比較適合來解釋資料:比較不同年齡組(達到法定飲酒年齡與否)的平均差異

Question 4

dta4<-read.table("dta4.txt",header=T)
colnames(dta4)<-c("Country","25_34","35_44","45_54","55_64","65_74")
head(dta4)
##   Country 25_34 35_44 45_54 55_64 65_74
## 1  Canada    22    27    31    34    24
## 2  Israel     9    19    10    14    27
## 3   Japan    22    19    21    31    49
## 4 Austria    29    40    52    53    69
## 5  France    16    25    36    47    56
## 6 Germany    28    35    41    49    52
str(dta4)
## 'data.frame':    15 obs. of  6 variables:
##  $ Country: Factor w/ 15 levels "Austria","Canada",..: 2 6 8 1 3 4 5 7 9 10 ...
##  $ 25_34  : int  22 9 22 29 16 28 48 7 8 26 ...
##  $ 35_44  : int  27 19 19 40 25 35 65 8 11 29 ...
##  $ 45_54  : int  31 10 21 52 36 41 84 11 18 36 ...
##  $ 55_64  : int  34 14 31 53 47 49 81 18 20 32 ...
##  $ 65_74  : int  24 27 49 69 56 52 107 27 28 28 ...
dta4 %>% janitor::adorn_totals("col") %>%
  gather(Age,cases,2:6) %>%
  ggplot(.,aes(reorder(Country,-cases),cases)) + 
  geom_boxplot() + theme_bw() +
  labs(x="Country",y="Deaths per 100,000 from male suicides")

Question 5

Question 6

Question 7

dta7<-read.table("dta7.txt",header=T)
ggplot(dta7,aes(beauty,eval)) + 
  geom_point() +
  facet_wrap(~courseID,ncol=6) + 
  theme_bw() +
  labs(x="Beauty judgment score",y="Average course evaluation score")