神Hadley R for Data Science の例題たちとその解答を書き残します。
今回はChapter 15 Factorsです。
過去の記事
- Chapter 3 データ可視化
- Chapter 5 データ変換
- Chapter 7 探索的データ分析
- Chapter 10 tibble
- Chapter 11 Data import
- Chapter 12 Tidy data
- Chapter 13 関係データ
- Chapter 14 文字列
この章では基本のtidyverse
に加えてforcats
ライブラリを使います。
library(tidyverse) library(forcats)
Chapter 15 Factors
15.3 General Social Survey
15.3.1 Exercises
1. Explore the distribution of
rincome
(reported income). What makes the default bar chart hard to understand? How could you improve the plot?
gss_cat %>% ggplot(aes(rincome)) + geom_bar() + theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
あえて言えばNot Applicableと$25000以上が大きすぎて他のカテゴリの大小がわかりにくいのだろうか。
縦軸を対数にすれば大小の比較はしやすくなった。
gss_cat %>% ggplot(aes(rincome)) + geom_bar() + theme(axis.text.x = element_text(angle = 90, vjust = 0.5)) + scale_y_log10()

2. What is the most common
relig
in this survey? What’s the most commonpartyid
?
gss_cat %>% count(relig, sort = TRUE) ## A tibble: 15 x 2 # relig n # <fct> <int> # 1 Protestant 10846 # 2 Catholic 5124 # 3 None 3523 # 4 Christian 689 # 5 Jewish 388 # 6 Other 224 # 7 Buddhism 147 # 8 Inter-nondenominational 109 # 9 Moslem/islam 104 #10 Orthodox-christian 95 #11 No answer 93 #12 Hinduism 71 #13 Other eastern 32 #14 Native american 23 #15 Don't know 15 gss_cat %>% count(partyid, sort = TRUE) ## A tibble: 10 x 2 # partyid n # <fct> <int> # 1 Independent 4119 # 2 Not str democrat 3690 # 3 Strong democrat 3490 # 4 Not str republican 3032 # 5 Ind,near dem 2499 # 6 Strong republican 2314 # 7 Ind,near rep 1791 # 8 Other party 393 # 9 No answer 154 #10 Don't know 1
3. Which
relig
doesdenom
(denomination) apply to? How can you find out with a table? How can you find out with a visualisation?
gss_cat %>% count(relig, denom, sort = TRUE) ## A tibble: 47 x 3 # relig denom n # <fct> <fct> <int> # 1 Catholic Not applicable 5124 # 2 None Not applicable 3523 # 3 Protestant Other 2534 # 4 Protestant Southern baptist 1536 # 5 Protestant Baptist-dk which 1457 # 6 Protestant No denomination 1224 # 7 Protestant United methodist 1067 # 8 Christian No denomination 452 # 9 Protestant Episcopal 397 #10 Jewish Not applicable 388 gss_cat %>% mutate(isna = denom == "Not applicable") %>% ggplot(aes(relig)) + geom_bar(aes(fill = isna), position = "fill") + theme(axis.text.x = element_text(angle = 90))
問答無用でProtestantがdenomination(宗派)があることが分かる。
15.4 Modifying factor order
15.4.1 Exercises
1. There are some suspiciously high numbers in
tvhours
. Is the mean a good summary?
gss_cat %>% ggplot(aes(tvhours)) + geom_histogram(color = "black") gss_cat %>% count(tvhours) %>% arrange(desc(tvhours)) ## A tibble: 25 x 2 # tvhours n # <int> <int> # 1 24 22 # 2 23 1 # 3 22 2 # 4 21 2 # 5 20 14 # 6 18 7 # 7 17 2 # 8 16 10 # 9 15 17 #10 14 24 ## ... with 15 more rows
20時間や24時間テレビを見続けるというのは明らかにおかしい解答と推測できる。
平均値の大小がこのような異常値がどれだけ含まれるかを反映して決定されてしまう可能性があるため危険である。
平均値でgeom_point
するのではなく、geom_boxplot
を使ってみる。
gss_cat %>% ggplot(aes(relig, tvhours)) + geom_boxplot() + coord_flip()
これを見ると次のようなことが分かりそう。
- ほどんどのreligで中間値は2時間だが、Other easternのみ1.5時間。Other easternはテレビを見る時間が短い
- 第一四分位点が高いのはProtestant, Catholic, Native american。これらはテレビを短時間しか見ない人が少ないことを表している
- 第三四分位点が低いのはHinduism, Other easternでこれらはテレビを長時間見る人が少ないことを表している
など
2. For each factor in
gss_cat
identify whether the order of the levels is arbitrary or principled.
gss_cat
のfactorはmaterial, race, rincome, partyid, relig, denom
で、このうち順序があるのはrincome
のみ。
それ以外は任意の順序になる。
3. Why did moving “Not applicable”, to the front of the levels move it to the bottom of the plot?
上を有効な解答で占めたいから。
下側にRefusedやDon’t knowなど無効な解答が集まっているので、Not applicableもそれと同じ下側に移した。
15.5 Modifying factor levels
15.5.1 Exercises
1. How have the proportions of people identifying as Democrat, Republican, and Independent changed over time?
gss_cat %>% mutate(partyid = fct_collapse(partyid, other = c("No answer", "Don't know", "Other party"), rep = c("Strong republican", "Not str republican"), ind = c("Ind,near rep", "Independent", "Ind,near dem"), dem = c("Not str democrat", "Strong democrat") )) %>% ggplot(aes(year)) + geom_bar(aes(fill = partyid), position = "fill")
rep(共和党)の比率が2005年から年々減ってきていることが分かる。
2. How could you collapse
rincome
into a small set of categories?
gss_cat %>% mutate(rincome = fct_collapse(rincome, na = c("No answer", "Don't know", "Refused", "Not applicable"), "Lt $10000" = c("$8000 to 9999", "$7000 to 7999", "$6000 to 6999", "$5000 to 5999", "$4000 to 4999", "$3000 to 3999", "$1000 to 2999", "Lt $1000") )) %>% count(rincome) ## A tibble: 6 x 2 # rincome n # <fct> <int> #1 na 8468 #2 $25000 or more 7363 #3 $20000 - 24999 1283 #4 $15000 - 19999 1048 #5 $10000 - 14999 1168 #6 Lt $10000 2153