Fetching data
library(WDI)
## Loading required package: RJSONIO
library(knitr)
library(countrycode)
dat <- WDI(indicator='NY.GDP.PCAP.KD', country=c('MX','CA','US','FI','RU'), start=1990, end=2012)
kable(head(dat[order(dat$year),]))
23 |
CA |
Canada |
27871.780 |
1990 |
46 |
FI |
Finland |
28796.525 |
1990 |
69 |
MX |
Mexico |
6525.852 |
1990 |
92 |
RU |
Russian Federation |
5685.047 |
1990 |
115 |
US |
United States |
32965.647 |
1990 |
22 |
CA |
Canada |
26912.186 |
1991 |
Converting countrynames into Finnish, Russian and Korean
library(countrycode)
dat$country.name.finnish <- countrycode(dat$iso2c, "iso2c", "country.name.finnish")
dat$country.name.russian <- countrycode(dat$iso2c, "iso2c", "country.name.russian")
dat$country.name.korean <- countrycode(dat$iso2c, "iso2c", "country.name.korean")
kable(head(dat[order(dat$year),]))
23 |
CA |
Canada |
27871.780 |
1990 |
Kanada |
Канада |
캐나다 |
46 |
FI |
Finland |
28796.525 |
1990 |
Suomi |
Финляндия |
핀란드 |
69 |
MX |
Mexico |
6525.852 |
1990 |
Meksiko |
Meкcикa |
멕시코 |
92 |
RU |
Russian Federation |
5685.047 |
1990 |
Venäjä |
Россия |
러시아 |
115 |
US |
United States |
32965.647 |
1990 |
Amerikan yhdysvallat |
Соединённые Штаты Америки |
아메리카 합중국 |
22 |
CA |
Canada |
26912.186 |
1991 |
Kanada |
Канада |
캐나다 |
Plotting data
library(ggplot2)
p <- ggplot(dat, aes(x=year,y=NY.GDP.PCAP.KD,group=country,color=country))
p <- p + geom_point() + geom_line()
p <- p + geom_text(data=merge(dat, aggregate(year ~ country.name.finnish, dat, max),
by=c("year","country.name.finnish")),
aes(x=year, y = NY.GDP.PCAP.KD,label=country.name.finnish),
hjust=-.1,vjust=-1,size=4)
p <- p + geom_text(data=merge(dat, aggregate(year ~ country.name.korean, dat, median),
by=c("year","country.name.korean")),
aes(x=year, y = NY.GDP.PCAP.KD,label=country.name.korean),
hjust=1,vjust=-1,size=4)
p <- p + geom_text(data=merge(dat, aggregate(year ~ country.name.russian, dat, min),
by=c("year","country.name.russian")),
aes(x=year, y = NY.GDP.PCAP.KD,label=country.name.russian),
hjust=1,vjust=-1,size=4)
p <- p + coord_cartesian(xlim=c(1980,2020))
p <- p + scale_x_continuous(breaks=c(1990:2012))
p <- p + theme_minimal() + theme(legend.position = "none") +
theme(text = element_text(family = "Open Sans", size= 12)) +
theme(axis.text.x = element_text(angle=90, vjust= 0.5))
p

sessionInfo()
## R version 3.1.2 (2014-10-31)
## Platform: x86_64-pc-linux-gnu (64-bit)
##
## locale:
## [1] LC_CTYPE=fi_FI.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=fi_FI.UTF-8 LC_COLLATE=fi_FI.UTF-8
## [5] LC_MONETARY=fi_FI.UTF-8 LC_MESSAGES=fi_FI.UTF-8
## [7] LC_PAPER=fi_FI.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=fi_FI.UTF-8 LC_IDENTIFICATION=C
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] ggplot2_1.0.0 countrycode_0.18 knitr_1.8 WDI_2.4
## [5] RJSONIO_1.3-0
##
## loaded via a namespace (and not attached):
## [1] colorspace_1.2-4 digest_0.6.4 evaluate_0.5.5 formatR_1.0
## [5] grid_3.1.2 gtable_0.1.2 htmltools_0.2.6 labeling_0.3
## [9] MASS_7.3-35 munsell_0.4.2 plyr_1.8.1 proto_0.3-10
## [13] Rcpp_0.11.3 reshape2_1.4.1 rmarkdown_0.3.3 scales_0.2.4
## [17] stringr_0.6.2 tools_3.1.2 yaml_2.1.13