#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$color)#算次數
## 
##    Cool Neutral    Warm 
##      37       7      22
proportions(table(x$color))#算百分比
## 
##      Cool   Neutral      Warm 
## 0.5606061 0.1060606 0.3333333
#問題:男女對顏色的喜好的差異?

#次數分配表
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)
p.t
##    
##      Cool Neutral  Warm
##   F 25.76    4.55 19.70
##   M 30.30    6.06 13.64
#畫長條圖
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", "gold"))

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

pie(m, main = "男生")

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

#畫圓餅圖並加上資料標籤
p.t
##    
##      Cool Neutral  Warm
##   F 25.76    4.55 19.70
##   M 30.30    6.06 13.64
pie_category <- colnames(p.t)
f_label <- paste(pie_category, f,"%", sep = "-")
f_label
## [1] "Cool-25.76-%"   "Neutral-4.55-%" "Warm-19.7-%"
m_label <- paste(pie_category, m,"%", sep = "-")
m_label
## [1] "Cool-30.3-%"    "Neutral-6.06-%" "Warm-13.64-%"
#par()# c(1,2),表示建立一個1x2的空間,用來呈現後續的圖
pie(f, labels = f_label, main = "Female")

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

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