21世紀の資本グラフ2

Quandl、xts、lattice、latticeExtra、gridExtra パッケージ

「21世紀の資本」のデータが公開されてるのでRを使ってグラフ化してみます。
(グラフもすでに公開されているのであまり意味はありません。)
(参考)
Piketty Codes
『21世紀の資本』日本語版サポートページ

Chapter 2: Growth: Illusions and Realities

World Growth Since the Industrial Revolution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
library(Quandl)
library(xts)
T2_1<-Quandl("PIKETTY/T2_1")
#並べ替え
sortlist <- order(T2_1[,1])
( dat <- T2_1[sortlist,] )
Date<-dat[,1]
#並べ替え(凡例をわかりやすくするため)
d0<-dat[,-1]
T2_1<-data.frame(Date,d0[order(d0[nrow(d0),], decreasing = TRUE)])
rownames(T2_1) <- c(1:nrow(T2_1))
#save(T2_1,file="T2_1.dat")
#load("T2_1.dat")
#xts classに変換
T2_1.xts <- as.xts(read.zoo(T2_1))
#png("pikettyT2_1.png",width=1200,height=800)
par(mar=c(5,5,5,6),las=1, cex.axis=0.8)
plot.zoo(T2_1.xts,main="World Growth Since the Industrial Revolution",plot.type="single",col=1:3,lwd=2,xlab="年",ylab="")
axis(4,T2_1.xts[nrow(T2_1.xts),],colnames(T2_1.xts),col.axis="black" )
#dev.off()

Demographic Growth since the Industrial Revolution
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
library(Quandl)
library(xts)
T2_3<-Quandl("PIKETTY/T2_3")
#並べ替え
sortlist <- order(T2_3[,1])
dat <- T2_3[sortlist,]
T2_3<-dat
rownames(T2_3) <- c(1:nrow(T2_3))
#save("T2_3", file="T2_3.dat")
#load("T2_3.dat")
#並べ替え(凡例をわかりやすくするため)
Date<-T2_3[,1]
d0<-T2_3[,-1]
T2_3<-data.frame(Date,d0[order(d0[nrow(d0),], decreasing = TRUE)])
rownames(T2_3) <- c(1:nrow(T2_3))
library(xts)
library(lattice)
library(latticeExtra)
T2_3.xts <- as.xts(read.zoo(T2_3))
#latticeExtraを使って装飾
#png("pikettyT2_3.png",width=1200,height=800)
asTheEconomist(
xyplot(T2_3.xts,superpose=TRUE,xlab="year",ylab="",main="Demographic Growth since the Industrial Revolution",lwd=2,grid = TRUE,
auto.key=list(title="", space = "right", cex=0.8, columns = 1), scales = list( y = list( rot = 0 )))
)
#dev.off()

Employment by Sector in France and the United States, 1800-2012
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
library(Quandl)
library(xts)
T2_4<-Quandl("PIKETTY/T2_4")
#並べ替え
sortlist <- order(T2_4[,1])
dat <- T2_4[sortlist,]
T2_4<-dat
rownames(T2_4) <- c(1:nrow(T2_4))
#save("T2_4", file="T2_4.dat")
#load("T2_4.dat")
#並べ替え(凡例をわかりやすくするため)
Date<-T2_4[,1]
d0<-T2_4[,-1]
T2_4<-data.frame(Date,d0[order(d0[nrow(d0),], decreasing = TRUE)])
rownames(T2_4) <- c(1:nrow(T2_4))
library(xts)
library(lattice)
library(latticeExtra)
T2_4.xts <- as.xts(read.zoo(T2_4))
#latticeExtraを使って装飾
#png("pikettyT2_4.png",width=1200,height=800)
xyplot(T2_4.xts,superpose=TRUE,xlab="year",ylab="",main="Employment by Sector in France and the United States, 1800-2012",lwd=2,grid = TRUE,
auto.key=list(title="", space = "right", cex=0.8, columns = 1), scales = list( y = list( rot = 0 )),par.settings = ggplot2like(), axis = axis.grid)
#dev.off()

World Growth from the Antiquity (growth rate per period)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
library(Quandl)
library(xts)
TS2_1<-Quandl("PIKETTY/TS2_1")
#並べ替え
sortlist <- order(TS2_1[,1])
( dat <- TS2_1[sortlist,] )
Date<-dat[,1]
#並べ替え(凡例をわかりやすくするため)
d0<-dat[,-1]
TS2_1<-data.frame(Date,d0[order(d0[nrow(d0),], decreasing = TRUE)])
rownames(TS2_1) <- c(1:nrow(TS2_1))
#save(TS2_1,file="TS2_1.dat")
#load("TS2_1.dat")
#xts classに変換
TS2_1.xts <- as.xts(read.zoo(TS2_1))
#png("pikettyTS2_1.png",width=1200,height=800)
par(mar=c(5,5,5,7),las=1, cex.axis=0.8)
plot.zoo(TS2_1.xts,main="World Growth from the Antiquity (growth rate per period)",plot.type="single",col=1:3,lwd=2,xlab="年",ylab="")
#最初の一つには最終データあり。残り二つはなし。
axis(4,TS2_1.xts[nrow(TS2_1.xts),1],colnames(TS2_1.xts[,1]),col.axis="black" )
axis(4,TS2_1.xts[nrow(TS2_1.xts)-2,-1],colnames(TS2_1.xts[,-1]),col.axis="black" )
#dev.off()

Growth Rate of World Population 1000-2100
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
library(Quandl)
library(xts)
TS2_2A<-Quandl("PIKETTY/TS2_2A")
#並べ替え
sortlist <- order(TS2_2A[,1])
dat <- TS2_2A[sortlist,]
TS2_2A<-dat
rownames(TS2_2A) <- c(1:nrow(TS2_2A))
#save("TS2_2A", file="TS2_2A.dat")
#load("TS2_2A.dat")
#並べ替え(凡例をわかりやすくするため)
Date<-TS2_2A[,1]
d0<-TS2_2A[,-1]
TS2_2A<-data.frame(Date,d0[order(d0[nrow(d0),], decreasing = TRUE)])
rownames(TS2_2A) <- c(1:nrow(TS2_2A))
library(xts)
library(lattice)
TS2_2A.xts <- as.xts(read.zoo(TS2_2A))
#png("TS2_2A.png",width=1200,height=800)
xyplot(TS2_2A.xts,superpose=TRUE,xlab="year",ylab="",main="Growth Rate of World Population 1000-2100",lwd=2,grid = TRUE,
auto.key=list(title="", space = "right", cex=0.8, columns = 1), scales = list( y = list( rot = 0 )))
#dev.off()

Detailed Series of World Population Growth Rate, 0-2100
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
library(Quandl)
library(xts)
TS2_2B<-Quandl("PIKETTY/TS2_2B")
#並べ替え
sortlist <- order(TS2_2B[,1])
dat <- TS2_2B[sortlist,]
TS2_2B<-dat
rownames(TS2_2B) <- c(1:nrow(TS2_2B))
#save("TS2_2B", file="TS2_2B.dat")
#load("TS2_2B.dat")
#並べ替え(凡例をわかりやすくするため)
Date<-TS2_2B[,1]
d0<-TS2_2B[,-1]
TS2_2B<-data.frame(Date,d0[order(d0[nrow(d0),], decreasing = TRUE)])
rownames(TS2_2B) <- c(1:nrow(TS2_2B))
library(xts)
library(lattice)
library(gridExtra)
TS2_2B.xts <- as.xts(read.zoo(TS2_2B))
#png("TS2_2B.png",width=1200,height=800)
#xyplot(TS2_2B.xts,superpose=TRUE,xlab="year",ylab="",main="Detailed Series of World Population Growth Rate, 1000-2100",lwd=2,grid = TRUE,
#auto.key=list(title="", space = "right", cex=0.8, columns = 1), scales = list( y = list( rot = 0 )))
p1<-xyplot(TS2_2B.xts[,c(2,6,8,12,13)],superpose=TRUE,xlab="year",ylab="",main="",lwd=2,grid = TRUE,
auto.key=list(title="", space = "top", cex=0.8, columns = 1), scales = list( y = list( rot = 0 )),ylim=c(-1,3))
p2<-xyplot(TS2_2B.xts[,c(1,3,5,10,11)],superpose=TRUE,xlab="year",ylab="",main="Detailed Series of World Population Growth Rate, 1000-2100",lwd=2,grid = TRUE,
auto.key=list(title="", space = "top", cex=0.8, columns = 1), scales = list( y = list( rot = 0 )),ylim=c(-1,3))
p3<-xyplot(TS2_2B.xts[,c(4,7,9,14,15)],superpose=TRUE,xlab="year",ylab="",main="",lwd=2,grid = TRUE,
auto.key=list(title="", space = "top", cex=0.8, columns = 1), scales = list( y = list( rot = 0 )),ylim=c(-1,3))
grid.arrange(p1,p2,p3,ncol=3)
#dev.off()

World Population 0-2100
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
library(Quandl)
library(xts)
TS2_2C<-Quandl("PIKETTY/TS2_2C")
TS2_2C
#日付の間違い2011-08-19 -> 0001-12-31
#14番めのデータ
TS2_2C[14,1]<-"0001-12-31"
#並べ替え
sortlist <- order(TS2_2C[,1])
( dat <- TS2_2C[sortlist,] )
Date<-dat[,1]
#並べ替え(凡例をわかりやすくするため)
d0<-dat[,-1]
TS2_2C<-data.frame(Date,d0[order(d0[nrow(d0),], decreasing = TRUE)])
rownames(TS2_2C) <- c(1:nrow(TS2_2C))
#Americaつづりが違う
names(TS2_2C)<-c("Date","World.population", "Asia","Africa","America","Europe")
#save(TS2_2C,file="TS2_2C.dat")
#load("TS2_2C.dat")
#xts classに変換
TS2_2C.xts <- as.xts(read.zoo(TS2_2C))
#png("pikettyTS2_2C.png",width=1200,height=800)
par(mar=c(5,5,5,7),las=1, cex.axis=0.8)
plot.zoo(TS2_2C.xts,main="World Population 0-2100",plot.type="single",col=1:3,lwd=2,xlab="年",ylab="")
axis(4,TS2_2C.xts[nrow(TS2_2C.xts),],colnames(TS2_2C.xts),col.axis="black" )
#dev.off()

Detailed Series of World Population 0-2100
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
library(Quandl)
library(xts)
TS2_2D<-Quandl("PIKETTY/TS2_2D")
TS2_2D
#日付の間違い2011-08-19 -> 0001-12-31
#14番めのデータ
TS2_2D[14,1]<-"0001-12-31"
#並べ替え
sortlist <- order(TS2_2D[,1])
dat <- TS2_2D[sortlist,]
TS2_2D<-dat
rownames(TS2_2D) <- c(1:nrow(TS2_2D))
#save(TS2_2D,file="TS2_2D.dat")
#load("TS2_2D.dat")
Date<-TS2_2D[,1]
#並べ替え(凡例をわかりやすくするため)
d0<-TS2_2D[,-1]
TS2_2D<-data.frame(Date,d0[order(d0[nrow(d0),], decreasing = TRUE)])
#xts classに変換
TS2_2D.xts <- as.xts(read.zoo(TS2_2D))
#png("pikettyTS2_2D.png",width=1200,height=800)
xyplot(TS2_2D.xts["1820-12-31::",-1],superpose=TRUE,xlab="year",ylab="",main="Detailed Series of World Population 1820-2100",lwd=2,grid = TRUE,
auto.key=list(title="", space = "right", cex=0.8, columns = 1), scales = list( y = list( rot = 0 )))
#dev.off()

Detailed Series for per capita Output Growth since the Industrial Revolution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
library(Quandl)
library(xts)
S2_3<-Quandl("PIKETTY/S2_3")
#並べ替え
sortlist <- order(S2_3[,1])
dat <- S2_3[sortlist,]
S2_3<-dat
rownames(S2_3) <- c(1:nrow(S2_3))
#save(S2_3,file="S2_3.dat")
#load("S2_3.dat")
Date<-S2_3[,1]
#並べ替え(凡例をわかりやすくするため)
d0<-S2_3[,-1]
S2_3<-data.frame(Date,d0[order(d0[nrow(d0),], decreasing = TRUE)])
#xts classに変換
S2_3.xts <- as.xts(read.zoo(S2_3))
#png("pikettyS2_3.png",width=1200,height=800)
xyplot(S2_3.xts,superpose=TRUE,xlab="year",ylab="",main="Detailed Series for per capita Output Growth since the Industrial Revolution",lwd=2,grid = TRUE,
auto.key=list(title="", space = "right", cex=0.8, columns = 1), scales = list( y = list( rot = 0 )))
#dev.off()

Inflation in Rich Countries Since the Industrial Revolution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
library(Quandl)
library(xts)
S2_5<-Quandl("PIKETTY/S2_5")
#並べ替え
sortlist <- order(S2_5[,1])
dat <- S2_5[sortlist,]
S2_5<-dat
rownames(S2_5) <- c(1:nrow(S2_5))
#save(S2_5,file="S2_5.dat")
#load("S2_5.dat")
Date<-S2_5[,1]
#並べ替え(凡例をわかりやすくするため)
d0<-S2_5[,-1]
S2_5<-data.frame(Date,d0[order(d0[nrow(d0),], decreasing = TRUE)])
#xts classに変換
S2_5.xts <- as.xts(read.zoo(S2_5))
#png("pikettyS2_5.png",width=1200,height=800)
xyplot(S2_5.xts,superpose=TRUE,xlab="year",ylab="",main="Inflation in Rich Countries Since the Industrial Revolution",lwd=2,grid = TRUE,
auto.key=list(title="", space = "right", cex=0.8, columns = 1), scales = list( y = list( rot = 0 )))
#dev.off()