forked from jtleek/capitalIn21stCenturyinR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter1.Rmd
65 lines (42 loc) · 2.39 KB
/
chapter1.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
Capital in the 21st Century: Chapter 0
========================================================
### Data provenance
The data were downloaded as Excel files from: http://piketty.pse.ens.fr/en/capital21c2.
### Loading relevant libraries and data
This document depends on the [xlsx](http://cran.r-project.org/web/packages/xlsx/index.html), [reshape2](http://cran.r-project.org/web/packages/reshape2/index.html), and [ggplot2](http://cran.r-project.org/web/packages/ggplot2/index.html) packages.
```{r loadCh0,message=FALSE}
library(ggplot2)
library(xlsx)
library(reshape2)
## Table TS1.1a
ts11a = read.xlsx("./Piketty2014FiguresTables/Chapter1TablesFigures.xlsx",sheetName="TS1.1a",rowIndex=8:18,colIndex=c(1,8:12),header=FALSE)
names(ts11a) = c("year","world","europe","america","africa","asia")
ts11a[,2:6] = ts11a[,2:6]/ts11a$world*100
ts11a=subset(ts11a,select=-world)
ts11a = melt(ts11a,id.vars="year")
names(ts11a) = c("year","Country","worldOutput")
## Table TS1.2
ts12 = read.xlsx("./Piketty2014FiguresTables/Chapter1TablesFigures.xlsx",sheetName="TS1.2",rowIndex=8:18,colIndex=c(1,8:12),header=FALSE)
names(ts12) = c("year","world","europe","america","africa","asia")
ts12[,2:6] = ts12[,2:6]/ts12$world*100
ts12=subset(ts12,select=-world)
ts12 = melt(ts12,id.vars="year")
names(ts12) = c("year","Country","worldPopulation")
## Table TS1.3
ts13 = read.xlsx("./Piketty2014FiguresTables/Chapter1TablesFigures.xlsx",sheetName="TS1.3",rowIndex=8:18,colIndex=c(1,10:14),header=FALSE)
names(ts13) = c("year","world","europe","america","africa","asia")
ts13$ea = (ts13$america + ts13$europe)/2
ts13$aa = (ts13$africa + ts13$asia)/2
ts13[,2:8] = ts13[,2:8]/ts13$world*100
ts13 = melt(ts13,id.vars="year")
```
## Make Figure 1.1
This code remakes Figure 1.1. Note that in the figure in the Excel tables, the x-axis is not evenly spaced, so the figure looks different.
```{r,dependson="loadCh0",fig.height=4,fig.width=8}
qplot(year,worldOutput,data=ts11a,fill=Country,geom="ribbon",position="stack",xlim=c(1700,2012),xlab="Year",ylab="Percent of World Output")
```
## Make Figure 1.2
This code remakes Figure 1.2. Note that in the figure in the Excel tables, the x-axis is not evenly spaced, so the figure looks different.
```{r,dependson="loadCh0",fig.height=4,fig.width=8}
qplot(year,worldPopulation,data=ts12,fill=Country,geom="ribbon",position="stack",xlim=c(1700,2012),xlab="Year",ylab="Percent of World Population")
```