実質経済成長率

ggplot2,Quandl パッケージ assign , eval(parse(text = “命令内容”)) コマンド

(参考)
Rで解析:連続処理に便利です。「規則的な変数名に値を代入」と「文字列でRに命令」する方法

Quandl パッケージを使うときassign , eval(parse(text = “命令内容”)) コマンドを使うと便利

Real GDP Growth By Country

G7 : フランス、アメリカ、イギリス、ドイツ、日本、イタリア、カナダ

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
library(ggplot2)
library(Quandl)
#
#assign , eval(parse(text = "命令内容")) コマンドを使わない。
#羅列していく。面倒くさい。
#USA=Quandl("ODA/USA_NGDP_RPCH", start_date="2000-01-01",end_date="2015-12-31", type="zoo")
#JPN=Quandl("ODA/JPN_NGDP_RPCH", start_date="2000-01-01",end_date="2015-12-31", type="zoo")
#DEU=Quandl("ODA/DEU_NGDP_RPCH", start_date="2000-01-01",end_date="2015-12-31", type="zoo")
#FRA=Quandl("ODA/FRA_NGDP_RPCH", start_date="2000-01-01",end_date="2015-12-31", type="zoo")
#GBR=Quandl("ODA/GBR_NGDP_RPCH", start_date="2000-01-01",end_date="2015-12-31", type="zoo")
#ITA=Quandl("ODA/ITA_NGDP_RPCH", start_date="2000-01-01",end_date="2015-12-31", type="zoo")
#CAN=Quandl("ODA/CAN_NGDP_RPCH", start_date="2000-01-01",end_date="2015-12-31", type="zoo")
#
#assign , eval(parse(text = "命令内容")) コマンド !!
#
country=c("USA","JPN","DEU","FRA","GBR","ITA","CAN")
#
for ( i in 1:length(country)) {
assign(country[i],
eval(parse(text =paste0("Quandl(\"ODA/",country[i],"_NGDP_RPCH\", start_date=\"2000-01-01\",end_date=\"2015-12-31\", type=\"zoo\")") )),
env = .GlobalEnv)
}
#
dat<-merge(USA,JPN,DEU,FRA,GBR,ITA,CAN)
#降順に並べ替え
sortlist<-order(dat[nrow(dat),],decreasing =T)
G7<-dat[,sortlist]
G7
#
# USA GBR DEU CAN FRA ITA JPN
#2000 4.092 3.799 3.194 5.183 3.875 3.710 2.257
#2001 0.976 2.758 1.843 1.771 1.954 1.772 0.355
#2002 1.786 2.494 0.016 3.010 1.118 0.249 0.290
#2003 2.807 3.337 -0.733 1.802 0.820 0.151 1.685
#2004 3.785 2.488 0.703 3.086 2.786 1.582 2.361
#2005 3.345 2.996 0.878 3.201 1.608 0.950 1.303
#2006 2.666 2.662 3.879 2.623 2.375 2.007 1.693
#2007 1.779 2.586 3.383 2.063 2.361 1.474 2.192
#2008 -0.292 -0.467 0.805 1.000 0.195 -1.050 -1.042
#2009 -2.776 -4.192 -5.565 -2.950 -2.941 -5.482 -5.527
#2010 2.532 1.540 3.945 3.083 1.966 1.687 4.711
#2011 1.602 1.972 3.718 3.141 2.079 0.577 -0.454
#2012 2.224 1.179 0.613 1.746 0.183 -2.819 1.742
#2013 1.489 2.160 0.406 2.218 0.656 -1.748 1.357
#2014 2.428 2.853 1.580 2.473 0.180 -0.343 -0.031
#2015 2.426 2.248 1.450 1.175 1.137 0.760 0.473
#
#png("G7realGDP.png",height=480,width=640)
ggplot(fortify(G7, melt = TRUE),aes(x = Index, y = Value, group = Series, colour = Series) ) +
geom_line()+
geom_point()+
labs(x="",y="",title="Real GDP Growth By Country",colour = "Country")
#dev.off()

G20

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
country=c("USA","CHN","JPN","DEU","FRA","BRA","GBR","ITA","RUS","IND","CAN","AUS","ESP","MEX","KOR","IDN","TUR","SAU","ARG","ZAF")
#
for ( i in 1:length(country)) {
assign(country[i],
eval(parse(text =paste0("Quandl(\"ODA/",country[i],"_NGDP_RPCH\", start_date=\"2000-01-01\",end_date=\"2015-12-31\", type=\"zoo\")") )),
env = .GlobalEnv)
}
#
dat<-merge(USA,CHN,JPN,DEU,FRA,BRA,GBR,ITA,RUS,IND,CAN,AUS,ESP,MEX,KOR,IDN,TUR,SAU,ARG,ZAF)
#降順に並べ替え
sortlist<-order(dat[nrow(dat),],decreasing =T)
G20<-dat[,sortlist]
#
#png("G20realGDP.png",height=480,width=640)
ggplot(fortify(G20, melt = TRUE),aes(x = Index, y = Value, group = Series, colour = Series) ) +
geom_line()+
geom_point()+
labs(x="",y="",title="Real GDP Growth By Country",colour = "Country")
#dev.off()