#0315上課資料

#install.packages("readr")#第一次使用需安裝
library(readr)#用library呼叫套件
## Warning: 套件 'readr' 是用 R 版本 4.2.3 來建造的
#讀取資料,命名為"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)#算次數
## 
##  F  M 
## 33 33
prop.table(table(x$gender))#算百分比
## 
##   F   M 
## 0.5 0.5
#問題:最喜歡的color、music、beverage、 drink、gender?
table(x$music)
## 
##       Electronic Folk/Traditional          Hip hop       Jazz/Blues 
##                8                4                8                4 
##              Pop     R&B and soul             Rock 
##               17                6               19
prop.table(table(x$music))
## 
##       Electronic Folk/Traditional          Hip hop       Jazz/Blues 
##       0.12121212       0.06060606       0.12121212       0.06060606 
##              Pop     R&B and soul             Rock 
##       0.25757576       0.09090909       0.28787879
#問題:男女對顏色的喜好的差異?

#次數分配表

t <- table(x$gender,x$color)

t
##    
##     Cool Neutral Warm
##   F   17       3   13
##   M   20       4    9
#百分比次數分配表
p.t <- proportions(t)
p.t
##    
##           Cool    Neutral       Warm
##   F 0.25757576 0.04545455 0.19696970
##   M 0.30303030 0.06060606 0.13636364
#將次數變成百分比(乘以100)
p.t <- p.t*100
  p.t
##    
##          Cool   Neutral      Warm
##   F 25.757576  4.545455 19.696970
##   M 30.303030  6.060606 13.636364
#四捨五入至小數2位        
p.t<- round(p.t,2)

s <- table(x$gender,x$music)
s
##    
##     Electronic Folk/Traditional Hip hop Jazz/Blues Pop R&B and soul Rock
##   F          2                2       1          3  13            2   10
##   M          6                2       7          1   4            4    9
p.t <- proportions(p.t,2)
p.t
##    
##          Cool   Neutral      Warm
##   F 0.4595077 0.4288407 0.5908818
##   M 0.5404923 0.5711593 0.4091182
p.t<- round(p.t,2)
p.t<- p.t*100
p.t
##    
##     Cool Neutral Warm
##   F   46      43   59
##   M   54      57   41
#畫長條圖
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("pink","purple")  )

#畫圓餅圖(畫兩個圖餅圖分別呈現男性與女性的某項喜好)
p.t
##    
##     Cool Neutral Warm
##   F   46      43   59
##   M   54      57   41
f <- p.t[1, ] # 取出女性資料
m <- p.t[2, ] # 取出男性資料
f
##    Cool Neutral    Warm 
##      46      43      59
m
##    Cool Neutral    Warm 
##      54      57      41
# par()是圖形控制函數,表示建立一個1x2的空間,用來呈現後續的圖
#par(mfrow = c(1,2) )
pie(f, main = "男生"  )

pie(m, main = "女生"  )

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

#畫圓餅圖並加上資料標籤
p.t
##    
##     Cool Neutral Warm
##   F   46      43   59
##   M   54      57   41
pie_category <- colnames(p.t)
f_label <- paste(pie_category, f,"%", sep = "")
f_label
## [1] "Cool46%"    "Neutral43%" "Warm59%"
m_label <- paste(pie_category, m,"%", sep = "")
m_label
## [1] "Cool54%"    "Neutral57%" "Warm41%"
#ar()# c(1,2),表示建立一個1x2的空間,用來呈現後續的圖
pie(f, labels = f_label, main = "Female")

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

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