library(readr)
#讀取資料,命名為"money"-----
#一、下載money_management.csv,讀入Rstudio,命名為money
#money <- read.csv("money_management.csv", stringsAsFactors = TRUE)
load("money_management.RData")
#二、快速檢視money的摘要資訊----
summary(money)
## id gender grade pocket_money savings household
## college: 66 女:116 一年級: 53 否:187 Min. : 0 否:209
## NDU :201 男:151 二年級:192 是: 80 1st Qu.: 2000 是: 58
## 三年級: 16 Median : 5000
## 四年級: 6 Mean : 5511
## 3rd Qu.: 8000
## Max. :30000
## keep_account
## 否:158
## 是:109
##
##
##
##
#三、不同年級(grade)人數的百分比為何?請依人數排列-----
sort(round(proportions(table(money$grade))*100, 2), decreasing = TRUE)
##
## 二年級 一年級 三年級 四年級
## 71.91 19.85 5.99 2.25
#四、不同年級(grade)的家用習慣差異為何?請以「長條圖」表示-----
p.t1 <- round(proportions(table(money$household, money$grade),2)*100 ,2)
p.t1
##
## 一年級 二年級 三年級 四年級
## 否 81.13 77.08 93.75 50.00
## 是 18.87 22.92 6.25 50.00
#畫長條圖
barplot(p.t1)

#畫分組長條圖
label <- rownames(p.t1)
barplot(p.t1,
legend.text = label,
col = c("cyan", "gold"),
main = "大學生家用習慣",
sub = "By Peter Liu",
xlab = "年級",
ylab = "人數")

#加上圖例與上色
label <- rownames(p.t1)
barplot(p.t1,
beside = TRUE,
legend.text = label,
col = c("cyan", "gold"),
main = "大學生家用習慣",
sub = "By Peter Liu",
xlab = "年級",
ylab = "人數")

#五、不同身份(id)的記帳習慣差異為何?請以「圖餅圖」表示-----
#畫圓餅圖
p.t2 <- round(proportions(table(money$id, money$keep_account),1)*100 ,2)
p.t2
##
## 否 是
## college 48.48 51.52
## NDU 62.69 37.31
college <- p.t2[1,]
college
## 否 是
## 48.48 51.52
ndu <- p.t2[2,]
ndu
## 否 是
## 62.69 37.31
#畫圓餅圖並加上資料標籤
pie_category <- colnames(p.t2)
pie_category
## [1] "否" "是"
college_label <- paste(pie_category, college,"%", sep = "")
ndu_label <- paste(pie_category, ndu,"%", sep = "")
college_label
## [1] "否48.48%" "是51.52%"
ndu_label
## [1] "否62.69%" "是37.31%"
par(mfrow = c(1,2))# c(1,2),表示建立一個1x2的空間,用來呈現後續的圖
pie(college, labels = college_label, main = "大學生記帳",sub = "By Peter Liu",col = c(2,5))
pie(ndu, labels = ndu_label, main = "ndu記帳",sub = "By Peter Liu" ,col = c(2,5))

dev.off() #離開par()
## null device
## 1