#請讀取superheroes.RData

load( file = "superheroes.RData")
#一、請將Alignment的資料型態轉換為facor, levels 依序為"good", "neutral", "bad"
str(superheroes$Alignment)
##  chr [1:428] "good" "good" "good" "good" "bad" "bad" "good" "good" "good" ...
summary(superheroes$Alignment)
##    Length     Class      Mode 
##       428 character character
superheroes$Alignment <- factor(superheroes$Alignment, 
                                levels = c("good", "neutral", "bad"))

#二、請計算Alignment裡的good", "neutral"和"bad"的佔比為何?
s <- table(superheroes$Alignment)
p.s <- proportions(s)
p.s <- p.s*100
p.s <- round(p.s, 2)
p.s
## 
##    good neutral     bad 
##   69.16    2.57   28.27
#三、請畫出Alignment的長條圖和圓餅圖。
par(mfrow = c(1,2)) 
barplot(p.s,
        col = c(7:9),
        main = "超級英雄",
        xlab = "陣營",
        ylab = "百分比")
pie(p.s,
    col= c(7:9),
    main = "超級英雄")

#四、請畫出speed的直方圖與盒狀圖。
boxplot(superheroes$Speed, main = "Speed")
hist(superheroes$Speed, main = "Speed")

#五、請計算Strength與Durability的相關係數,並畫出散佈圖。
cor(superheroes$Strength, superheroes$Durability)
## [1] 0.6843862
plot(superheroes$Strength, superheroes$Durability, 
     pch = 16, 
     col = "orange",
     xlab = "Strength",
     ylab = "Durability",
     main = "Strength/Durability", 
     sub = "Peter Liu")

#六、請問好人們和壞人們誰比較強?
y <- tapply(superheroes$Total, superheroes$Alignment, mean)
y
##     good  neutral      bad 
## 310.5946 355.3636 335.0331
barplot(y, col = c(2:4),
        xlab = "陣營",
        ylab = "Average Ability",
        main = "超級英雄比一比", 
        sub = "Peter Liu")