Last active
September 24, 2015 11:35
-
-
Save m-note/d4f40287de7531e8e050 to your computer and use it in GitHub Desktop.
エリアプロット
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- | |
| title: "Polity Line Graph" | |
| date: "2015年9月24日" | |
| output: html_document | |
| --- | |
| # まずパッケージを読み込む | |
| ```{r} | |
| library(ggplot2) | |
| library(foreign) | |
| ``` | |
| # データの読み込み | |
| ダウンロードしたSPSSファイルを読み込む (データフレーム形式にする) | |
| ```{r} | |
| data <- data.frame(read.spss("p4v2014.sav")) | |
| ``` | |
| 読み込んだデータはこんな感じ | |
| ```{r} | |
| head(data, 3) | |
| ``` | |
| # ggplot2で図を作っていく | |
| 目標としては、数値ごとにPolityを分けた上で3つの折れ線グラフを作ること | |
| - autocracies: -10 から -6 | |
| - anocracies: -5 から 5 | |
| - democracies 6 から 10 | |
| 方針としては、 | |
| - 上の基準にそってdata$regimeに分類を記す | |
| - それを年ごとにまとめる | |
| - グラフにする | |
| ### まず分類する | |
| autocraciesについて | |
| ```{r} | |
| library(dplyr) | |
| autocracies <- data %>% | |
| dplyr::filter(polity2 >= -10 & polity2 <= -6) | |
| autocracies$regime <- "autocracy" | |
| ``` | |
| anocraciesについて | |
| ```{r} | |
| anocracies <- data %>% | |
| dplyr::filter(polity2 >= -5 & polity2 <= 5) | |
| anocracies$regime <- "anocracy" | |
| ``` | |
| democraciesについて | |
| ```{r} | |
| democracies <- data %>% | |
| dplyr::filter(polity2 >= 6 & polity2 <= 10) | |
| democracies$regime <- "democracy" | |
| ``` | |
| まとめる | |
| ```{r} | |
| data2 <- rbind(autocracies, anocracies, democracies) | |
| ``` | |
| ### 年ごと、体制ごとにまとめる | |
| ```{r} | |
| data3 <- data2 %>% | |
| dplyr::group_by(year, regime) %>% | |
| dplyr::summarise(n=n()) | |
| ``` | |
| 割合を求めるために、ある年にある国の合計を求める | |
| ```{r} | |
| year_total <- data2 %>% | |
| dplyr::group_by(year) %>% | |
| dplyr::summarise(total=n()) | |
| ``` | |
| data3とまとめ、割合を求める | |
| ```{r} | |
| data3$total <- NA | |
| for(i in 1:nrow(data3)){ | |
| year = as.integer(data3[i, "year"]) | |
| line_num = year - 1799 | |
| data3[i, "total"] <- as.integer(year_total[line_num, "total"]) | |
| } | |
| data3$proportion <- (data3$n/data3$total)*100 | |
| ``` | |
| ### 積み上げ折れ線グラフ(エリアプロット)を作る | |
| 参考: <http://ill-identified.hatenablog.com/entry/2014/02/22/184651> | |
| ```{r} | |
| library(reshape2) | |
| g <- ggplot(data3) + | |
| theme_bw() + | |
| geom_line(mapping=aes(x=year, y=proportion, group=regime, color=regime, ymax=100), position="stack") + | |
| geom_ribbon(mapping=aes(x=year, y=proportion, ymin=0, ymax=100, group=regime, fill=regime), position="stack", alpha=.7) + | |
| theme(axis.text.x = element_text(size=12), # グラフのタイトルのサイズなど | |
| axis.title.x = element_text(size=15), | |
| axis.text.y = element_text(size=12), | |
| axis.title.y = element_text(size=15), | |
| axis.ticks.y = element_blank(), | |
| plot.title = element_text(size = rel(1.4))) + | |
| labs(title = "Global Trends of Polity Score", x="year", y="proportion") + | |
| scale_fill_discrete(name="Polity Score", # colour, fill, shape今回は色塗りに対応してだったからfill? | |
| breaks=c("anocracy", "autocracy", "democracy"), | |
| labels=c("anocracy (-5 to 5)", "autocracy (-10 to -6)", "democracy (6 to 10)"), | |
| guide = guide_legend(reverse=T)) + | |
| scale_linetype_discrete(guide=F) + | |
| scale_color_discrete(guide=F) # これで凡例から線が消える | |
| g | |
| ``` | |
| 別パターン: | |
| 割合じゃなくて、単純に国の数で示すことにする | |
| スコア順に表示したいので、dplyrの方で並び順を変えてみる --> でも上手くいかなかった | |
| ```{r} | |
| head(data3) | |
| data4 <- data3 %>% | |
| dplyr::arrange(desc(regime), desc(proportion)) | |
| ``` | |
| ```{r} | |
| library(reshape2) | |
| g <- ggplot(data3) + | |
| theme_bw() + | |
| geom_line(mapping=aes(x=year, y=n, group=regime, color=regime, ymax=n), position="stack") + | |
| geom_ribbon(mapping=aes(x=year, y=n, ymin=0, ymax=n, group=regime, fill=regime), position="stack", alpha=.7) + | |
| theme(axis.text.x = element_text(size=12), # グラフのタイトルのサイズなど | |
| axis.title.x = element_text(size=15), | |
| axis.text.y = element_text(size=12), | |
| axis.title.y = element_text(size=15), | |
| axis.ticks.y = element_blank(), | |
| plot.title = element_text(size = rel(1.4))) + | |
| labs(title = "Global Trends of Polity Score", x="year", y="proportion") + | |
| scale_fill_discrete(name="Polity Score", # colour, fill, shapeの使い分けがよくわからず。今回は色塗りに対応してだったからfill? | |
| # cf.http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/ | |
| breaks=c("democracy", "autocracy", "anocracy" ), | |
| labels=c("6 to 10 (democracy)", "-10 to -6 (autocracy)", "-5 to 5 (anocracy)"), | |
| guide = guide_legend(reverse=T)) + | |
| scale_linetype_discrete(guide=F) + | |
| scale_color_discrete(guide=F) # これで凡例から線が消える | |
| g | |
| ``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment