#0315上課資料

#install.packages("readr")#第一次使用需安裝
library(readr)#用library呼叫套件
#讀取資料,命名為"x"
x <- read.csv("favorite.csv", stringsAsFactors = TRUE)

#查看資料結構
summary(x)
##  Favorite.Color       Favorite.Music.Genre     Favorite.Beverage
##  Cool   :37     Electronic      : 8        Beer         :13     
##  Neutral: 7     Folk/Traditional: 4        Doesn't drink:14     
##  Warm   :22     Hip hop         : 8        Other        :11     
##                 Jazz/Blues      : 4        Vodka        : 9     
##                 Pop             :17        Whiskey      : 9     
##                 R&B and soul    : 6        Wine         :10     
##                 Rock            :19                             
##       Favorite.Soft.Drink Gender
##  7UP/Sprite     :13       F:33  
##  Coca Cola/Pepsi:32       M:33  
##  Fanta          :14             
##  Other          : 7             
##                                 
##                                 
## 
#重新命名欄位名稱
colnames(x) #檢視欄位名稱
## [1] "Favorite.Color"       "Favorite.Music.Genre" "Favorite.Beverage"   
## [4] "Favorite.Soft.Drink"  "Gender"
colnames(x) <- c("color", "music", "beverage", "drink", "gender")     

###類別資料分析#table(x$gender)#算次數
prop.table(table(x$gender))#算百分比##
## 
##   F   M 
## 0.5 0.5
#問題:男女生各有多少人?
table(x$gender)#算次數
## 
##  F  M 
## 33 33
prop.table(table(x$gender))#算百分比
## 
##   F   M 
## 0.5 0.5
#問題:最喜歡的color、music、beverage、 drink、gender?
table(x$drink)#算次數
## 
##      7UP/Sprite Coca Cola/Pepsi           Fanta           Other 
##              13              32              14               7
prop.table(table(x$drink))#算百分比
## 
##      7UP/Sprite Coca Cola/Pepsi           Fanta           Other 
##       0.1969697       0.4848485       0.2121212       0.1060606
#問題:男女對顏色的喜好的差異?

#次數分配表
t <- table(x$gender, x$drink)
t
##    
##     7UP/Sprite Coca Cola/Pepsi Fanta Other
##   F          8              17     6     2
##   M          5              15     8     5
#百分比次數分配表
p.t <- proportions(t)
p.t
##    
##     7UP/Sprite Coca Cola/Pepsi      Fanta      Other
##   F 0.12121212      0.25757576 0.09090909 0.03030303
##   M 0.07575758      0.22727273 0.12121212 0.07575758
#將次數變成百分比(乘以100)
p.t <-p.t*100
p.t
##    
##     7UP/Sprite Coca Cola/Pepsi     Fanta     Other
##   F  12.121212       25.757576  9.090909  3.030303
##   M   7.575758       22.727273 12.121212  7.575758
#四捨五入至小數2位
p.t <- round(p.t, 2)
p.t
##    
##     7UP/Sprite Coca Cola/Pepsi Fanta Other
##   F      12.12           25.76  9.09  3.03
##   M       7.58           22.73 12.12  7.58
#畫長條圖
barplot(p.t)

#畫分組長條圖
barplot(p.t, beside =TRUE    )

#加上圖例與上色
rownames(p.t)
## [1] "F" "M"
label <- rownames(p.t)
label
## [1] "F" "M"
barplot(p.t,
        beside = TRUE,
        legend.text =  label ,
        col = c("blue","orange")   )

#畫圓餅圖(畫兩個圖餅圖分別呈現男性與女性的某項喜好)
p.t
##    
##     7UP/Sprite Coca Cola/Pepsi Fanta Other
##   F      12.12           25.76  9.09  3.03
##   M       7.58           22.73 12.12  7.58
f <- p.t[1,] # 取出女性資料
m <- p.t[2,] # 取出男性資料
f
##      7UP/Sprite Coca Cola/Pepsi           Fanta           Other 
##           12.12           25.76            9.09            3.03
m
##      7UP/Sprite Coca Cola/Pepsi           Fanta           Other 
##            7.58           22.73           12.12            7.58
# par()是圖形控制函數,mfrow = c(1,2)表示建立一個1x2的空間,用來呈現後續的圖
#par(mfrow =   )
pie(f, main = "女"  )

pie(m, main =  "男" )

#dev.off()  #離開par()

#畫圓餅圖並加上資料標籤
p.t
##    
##     7UP/Sprite Coca Cola/Pepsi Fanta Other
##   F      12.12           25.76  9.09  3.03
##   M       7.58           22.73 12.12  7.58
pie_category <- colnames(p.t)
f_label <- paste(pie_category, f,"%", sep = "")
f_label
## [1] "7UP/Sprite12.12%"      "Coca Cola/Pepsi25.76%" "Fanta9.09%"           
## [4] "Other3.03%"
m_label <- paste(pie_category, m,"%", sep = "")
m_label
## [1] "7UP/Sprite7.58%"       "Coca Cola/Pepsi22.73%" "Fanta12.12%"          
## [4] "Other7.58%"
#par()# c(1,2),表示建立一個1x2的空間,用來呈現後續的圖
pie(f, labels = f_label, main = "Female")

pie(m, labels = m_label, main = "Male" )

#dev.off()  #離開par()