加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

R语言如何调用世界银行数据来比较GDP

(2012-04-20 08:09:20)
标签:

r语言

世界银行数据

数据分析

it

分类: DataScience

调用世界银行数据主要是用到WDI包,直接读取数据。
WDI包简介:WDI(World Development Indicators (World Bank)):世界发展指标(世界银行),搜索、提取和格式化从来自世界银行的“世界发展指标”数据。

WDI使用说明

打开RGui或者RStudio,需要安装两个包WDI和ggplot2


> install.packages(c("WDI","ggplot2"),.Library)
用WDI包的WDI函数,将中国、俄罗斯、南非、印度、巴西这五国的GDP数据载入,时间为1990-2010年,在选择国家时需使用ISO-2标准的国家代码,数据指标是世界银行的特定编码,你可以通过WDIsearch("gdp")命令来得到所有GDP的编码。

> WDIsearch(string='gdp', field='N', indicator='NY.GDP.MKTP.KD.ZG')

                   Series.Code           Series.Name
749 NY.GDP.MKTP.KD.ZG GDP growth (annual %)

......

> library(ggplot2)
> library(WDI)

>DF = WDI(country=c("CN","RU","BR","ZA","IN"),indicator="NY.GDP.MKTP.KD.ZG",start=1990, end=2010)

查看数据,只截取中国的GDP增长
> DF[c(DF$country=="China"),]

   country iso2c year NY.GDP.MKTP.KD.ZG
21   China    CN 1990               3.8
20   China    CN 1991               9.2
19   China    CN 1992              14.2
18   China    CN 1993              14.0
17   China    CN 1994              13.1
16   China    CN 1995              10.9
15   China    CN 1996              10.0
14   China    CN 1997               9.3
13   China    CN 1998               7.8
12   China    CN 1999               7.6
11   China    CN 2000               8.4
10   China    CN 2001               8.3
   China    CN 2002               9.1
   China    CN 2003              10.0
   China    CN 2004              10.1
   China    CN 2005              11.3
   China    CN 2006              12.7
   China    CN 2007              14.2
   China    CN 2008               9.6
   China    CN 2009               9.2
   China    CN 2010              10.4

用ggplot2包绘图,不同颜色代表不同国家的数据
>ggplot(DF, aes(year, NY.GDP.MKTP.KD.ZG, color=country))+geom_line()+geom_point()+xlab("Year")+ opts(title="Annual GDP Growth rate (%)")+ylab("")
也可以修改一下背景颜色:
>ggplot(DF, aes(year, NY.GDP.MKTP.KD.ZG, color=country))+geom_line()+theme_bw()+geom_point()+xlab("Year")+ opts(title="Annual GDP Growth rate (%)")+ylab("")
> ggsave(file='data.png')

得到NY.GDP.MKTP.KD.ZG GDP 是描述GDP的年增长。

> WDIsearch(string='gdp', field='N', indicator='NY.GDP.MKTP.KD.ZG')
          Series.Code           Series.Name
749 NY.GDP.MKTP.KD.ZG GDP growth (annual %) 



参考文章(需要翻墙):
1.   World Bank API R package available!
2.  R语言中如何调用世界银行数据来比较金砖五国的GDP





0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有