🏠 學習重點：
■ 條狀圖簡單易懂，是很重要的的分析、溝通工具
■ 點狀圖通常用在比較各分析對象
■ 條狀圖主要用在分類比較
■ ggplot的三種條狀圖
◇ geom_bar() : 類似 barplot()
◇ geom_col() : 類似 table() %>% barplot()
◇ geom_histogram() : hist()
■ 有兩個分類變數時，需要設定position來選擇比較的基礎
"stack" : 比較大類總數
"dodge" : 比較各細類數量
"fill" : 比較細類比率

### 1. 三種不一樣的條狀圖

##### 1.1 準備資料

pacman::p_load(dplyr,ggplot2,plotly,gridExtra)
theme_set(theme_get() + theme(
text=element_text(size=8), legend.key.size=unit(10,"points")
))

D = read.csv("data/comics1.csv",stringsAsFactors=F) %>% as_tibble
glimpse(D)
Rows: 7,250
Columns: 9
$publisher <chr> "dc", "dc", "dc", "dc", "dc", "dc", "dc", "dc", "dc", "dc"~$ name        <chr> "Batman (Bruce Wayne)", "Superman (Clark Kent)", "Green La~
align <chr> "Good", "Good", "Good", "Good", "Good", "Good", "Good", "G~ eye         <chr> "Blue", "Blue", "Brown", "Brown", "Blue", "Blue", "Blue", ~
$hair <chr> "Black", "Black", "Brown", "White", "Black", "Black", "Blo~$ sex         <chr> "Male", "Male", "Male", "Male", "Male", "Female", "Male", ~
$alive <chr> "Living", "Living", "Living", "Living", "Living", "Living"~$ appearances <int> 3093, 2496, 1565, 1316, 1237, 1231, 1121, 1095, 1075, 1028~
table(Ddecade)  3 4 5 6 7 8 9 10 11 28 271 106 678 823 1304 1581 1803 656  ##### 4.1 窗格功能 facet 好壞人的數量如何隨年代而變 ❓ 好壞人數量的變化趨勢是否因性別、出版商而異 ❓ facet_grid(var1~var2)可以就每一種(v1,v2)的組合畫一個窗格 ggplot(D, aes(x=decade, fill=align)) + geom_bar() + facet_grid(sex~publisher)  我們用兩行簡單的程式碼就可以回答這些看似複雜繁瑣的問題了！ 通常，比例會比數量更能反映出趨勢，欲把數字轉換為比例， 我們只需要將 position='fill 放在 geom_bar() 中。 # here we convert align into a factor so we can # re-order the align levels in a desirable way D2 = D %>% mutate( align=factor(align,levels=c("Bad","Neutral","Good")) ) %>% filter(decade >= 6) ggplot(D2, aes(x=decade, fill=align)) + geom_bar(position="fill") + facet_grid(sex~publisher) ##### 4.2 比例 vs. 數量 下面是一個條形圖，顯示了在不同頭髮顏色和不同眼睛顏色的搭配中，好人和壞人所佔的比例。 hx = count(D2, hair, sort=T) ex = count(D2, eye, sort=T) D2 %>% filter( hair%in%hxhair[1:3], eye%in%ex\$eye[1:3],
align!="Neutral") %>%
facet_grid(hair~eye)