Healy(2019)読書メモ(2)

ch.5(https://socviz.co/workgeoms.html#continuous-variables-by-group-or-category)

 

text p.111にある

> by_country <- organdata %>% group_by(consent_law, country) %>%
+ summarize_if(is.numeric, funs(mean, sd), na.rm = TRUE) %>%
+ ungroup()

を実行すると、

Warning message:
funs() is soft deprecated as of dplyr 0.8.0
please use list() instead

# Before:
funs(name = f(.)

# After:
list(name = ~f(.))
This warning is displayed once per session.

こんなWarningがでます。新しいdplyrを使うときは、

> by_country <- organdata %>% group_by(consent_law, country) %>%
+ summarize_if(is.numeric, list(mean, sd), na.rm = TRUE) %>%
+ ungroup()

 とやる方がよさそうです。

そしてこれによって生成される変数が、教科書ではdonors_meanなどになっていますが、実際にはdonors_fn1で出てきました。ということはFigure 5.13, 5.14を作る際に、

meanのところをfn1に置換してやらないとエラーとなり、図は作成されません。

 同様に、Cleveland dot plotを作るためのスクリプト(p.115)も

p <- ggplot(data = by_country, mapping = aes(x = reorder(country,
donors_mean), y = donors_mean))

p + geom_pointrange(mapping = aes(ymin = donors_mean - donors_sd,
ymax = donors_mean + donors_sd)) +
labs(x= "", y= "Donor Procurement Rate") + coord_flip()

 meanをfn1に、sdをfn2に置換する必要があります。 

このような修正はby_countryをデータに使う場合に常に必要です。