library(readr)#用library呼叫套件
#讀取資料,命名為"x"
x <- read.csv("strays.csv", stringsAsFactors = TRUE)

summary(x)
##        id         type      gender      size        color        age     
##  Min.   : 26250   狗:1048   F:534   BIG   :181   咖啡色: 13   ADULT:886  
##  1st Qu.: 50182   貓:  34   M:548   MEDIUM:713   白色  : 26   CHILD:196  
##  Median :129335                     SMALL :188   花色  : 88              
##  Mean   :120020                                  虎斑色:104              
##  3rd Qu.:179406                                  黃色  :249              
##  Max.   :248935                                  黑色  :602              
##                                                                          
##      county   
##  新北市 :220  
##  南投縣 :188  
##  高雄市 :172  
##  臺南市 : 97  
##  臺北市 : 84  
##  桃園市 : 74  
##  (Other):247
colnames(x)
## [1] "id"     "type"   "gender" "size"   "color"  "age"    "county"
colnames(x) <- c("id", "type", "gender", "size", "color","age","county")

table(x$type)
## 
##   狗   貓 
## 1048   34
prop.table(table(x$type))
## 
##         狗         貓 
## 0.96857671 0.03142329
table(x$size)
## 
##    BIG MEDIUM  SMALL 
##    181    713    188
prop.table(table(x$size))
## 
##       BIG    MEDIUM     SMALL 
## 0.1672828 0.6589649 0.1737523
g<- table(x$type, x$size)
g.b <- prop.table(g,1)
g.b <- g.b*100
g.b<- round(g.b,2)
barplot(g.b)

barplot(g.b, beside =  T  )

rownames(g.b)
## [1] "狗" "貓"
label <- rownames(g.b)
label
## [1] "狗" "貓"
barplot(g.b,
        beside = T,
        legend.text =  label ,
        col = c(2,5),
        main = "犬與貓的體型分析",
        sub = "By 1140730323徐翊芳")

x <- read.csv("strays.csv", stringsAsFactors = TRUE)
summary(x)
##        id         type      gender      size        color        age     
##  Min.   : 26250   狗:1048   F:534   BIG   :181   咖啡色: 13   ADULT:886  
##  1st Qu.: 50182   貓:  34   M:548   MEDIUM:713   白色  : 26   CHILD:196  
##  Median :129335                     SMALL :188   花色  : 88              
##  Mean   :120020                                  虎斑色:104              
##  3rd Qu.:179406                                  黃色  :249              
##  Max.   :248935                                  黑色  :602              
##                                                                          
##      county   
##  新北市 :220  
##  南投縣 :188  
##  高雄市 :172  
##  臺南市 : 97  
##  臺北市 : 84  
##  桃園市 : 74  
##  (Other):247
colnames(x)
## [1] "id"     "type"   "gender" "size"   "color"  "age"    "county"
colnames(x) <- c("id", "type", "gender", "size", "color","age","county")

table(x$type)
## 
##   狗   貓 
## 1048   34
prop.table(table(x$county))
## 
##       南投縣       嘉義市       嘉義縣       基隆市       宜蘭縣       屏東縣 
## 0.1737523105 0.0018484288 0.0009242144 0.0138632163 0.0027726433 0.0369685767 
##       彰化縣       新北市       新竹市       新竹縣       桃園市       澎湖縣 
## 0.0036968577 0.2033271719 0.0582255083 0.0055452865 0.0683918669 0.0138632163 
##       臺中市       臺北市       臺南市       臺東縣       花蓮縣       苗栗縣 
## 0.0101663586 0.0776340111 0.0896487985 0.0027726433 0.0018484288 0.0027726433 
##       金門縣       雲林縣       高雄市 
## 0.0452865065 0.0277264325 0.1589648799
table(x$county)
## 
## 南投縣 嘉義市 嘉義縣 基隆市 宜蘭縣 屏東縣 彰化縣 新北市 新竹市 新竹縣 桃園市 
##    188      2      1     15      3     40      4    220     63      6     74 
## 澎湖縣 臺中市 臺北市 臺南市 臺東縣 花蓮縣 苗栗縣 金門縣 雲林縣 高雄市 
##     15     11     84     97      3      2      3     49     30    172
prop.table(table(x$scounty))
## numeric(0)
g<- table(x$type, x$county)
g.b <- prop.table(g,1)
g.b <- g.b*100
g.b<- round(g.b,2)
barplot(g.b)

barplot(g.b, beside =  T  )

rownames(g.b)
## [1] "狗" "貓"
label <- rownames(g.b)
label
## [1] "狗" "貓"
barplot(g.b,
        beside = T,
        legend.text =  label ,
        col = c(9,7),
        main = "各縣市流浪動物數量",
        sub = "By1140730323徐翊芳")

table(x$type)
## 
##   狗   貓 
## 1048   34
prop.table(table(x$size))
## 
##       BIG    MEDIUM     SMALL 
## 0.1672828 0.6589649 0.1737523
table(x$county)
## 
## 南投縣 嘉義市 嘉義縣 基隆市 宜蘭縣 屏東縣 彰化縣 新北市 新竹市 新竹縣 桃園市 
##    188      2      1     15      3     40      4    220     63      6     74 
## 澎湖縣 臺中市 臺北市 臺南市 臺東縣 花蓮縣 苗栗縣 金門縣 雲林縣 高雄市 
##     15     11     84     97      3      2      3     49     30    172
prop.table(table(x$size))
## 
##       BIG    MEDIUM     SMALL 
## 0.1672828 0.6589649 0.1737523
g<- table(x$type, x$size)
g.b <- prop.table(g,1)
g.b <- g.b*100
g.b<- round(g.b,2)
barplot(g.b)

barplot(g.b, beside =  T  )

rownames(g.b)
## [1] "狗" "貓"
label <- rownames(g.b)
label
## [1] "狗" "貓"
barplot(g.b,
        beside = T,
        legend.text =  label ,
        col = c(7,19),
        main = "犬與貓的體型分析",
        sub = "By1140730323徐翊芳")

c<-table(x$type,x$color)
c
##     
##      咖啡色 白色 花色 虎斑色 黃色 黑色
##   狗     13   26   75     93  246  595
##   貓      0    0   13     11    3    7
p.c <- prop.table(c)
p.c
##     
##           咖啡色        白色        花色      虎斑色        黃色        黑色
##   狗 0.012014787 0.024029575 0.069316081 0.085951941 0.227356747 0.549907579
##   貓 0.000000000 0.000000000 0.012014787 0.010166359 0.002772643 0.006469501
#將次數變成百分比(乘以100)
p.c <-p.c*100 
p.c
##     
##          咖啡色       白色       花色     虎斑色       黃色       黑色
##   狗  1.2014787  2.4029575  6.9316081  8.5951941 22.7356747 54.9907579
##   貓  0.0000000  0.0000000  1.2014787  1.0166359  0.2772643  0.6469501
#四捨五入至小數2位
p.c <- round(p.c,2)
p.c
##     
##      咖啡色  白色  花色 虎斑色  黃色  黑色
##   狗   1.20  2.40  6.93   8.60 22.74 54.99
##   貓   0.00  0.00  1.20   1.02  0.28  0.65
d <- p.c[1,] # 女性資料
a <- p.c[2,] # 男性資料
d
## 咖啡色   白色   花色 虎斑色   黃色   黑色 
##   1.20   2.40   6.93   8.60  22.74  54.99
a
## 咖啡色   白色   花色 虎斑色   黃色   黑色 
##   0.00   0.00   1.20   1.02   0.28   0.65
# par()是圖形控制函數,mfrow = c(1,2)表示建立一個1x2的空間,用來呈現後續的圖1列2欄
par(mfrow = c(1,2)   )
pie(d, main ="狗"   )
pie(a, main = "貓")

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