# search and plot world bank data
library(WDI)
## Warning: package 'WDI' was built under R version 3.2.3
## Loading required package: RJSONIO
## Warning: package 'RJSONIO' was built under R version 3.2.3
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.2.5
library(countrycode)
## Warning: package 'countrycode' was built under R version 3.2.5
WDI : Downloads the requested data by using the World Bank’s API, parses the resulting JSON file, and formats it in long country-year format. countrycode : Converts long country names into one of many different coding schemes.
iMData <- WDIsearch("Fertility rate", field = "name", short = FALSE)
iMData
## indicator
## [1,] "SP.ADO.TFRT"
## [2,] "SP.DYN.TFRT.IN"
## [3,] "SP.DYN.WFRT"
## name
## [1,] "Adolescent fertility rate (births per 1,000 women ages 15-19)"
## [2,] "Fertility rate, total (births per woman)"
## [3,] "Wanted fertility rate (births per woman)"
## description
## [1,] "Adolescent fertility rate is the number of births per 1,000 women ages 15-19."
## [2,] "Total fertility rate represents the number of children that would be born to a woman if she were to live to the end of her childbearing years and bear children in accordance with current age-specific fertility rates."
## [3,] "Wanted fertility rate is an estimate of what the total fertility rate would be if all unwanted births were avoided."
## sourceDatabase
## [1,] "World Development Indicators"
## [2,] "World Development Indicators"
## [3,] "World Development Indicators"
## sourceOrganization
## [1,] "United Nations Population Division, World Population Prospects."
## [2,] "(1) United Nations Population Division. World Population Prospects, (2) United Nations Statistical Division. Population and Vital Statistics Reprot (various years), (3) Census reports and other statistical publications from national statistical offices, (4) Eurostat: Demographic Statistics, (5) Secretariat of the Pacific Community: Statistics and Demography Programme, and (6) U.S. Census Bureau: International Database."
## [3,] "Demographic and Health Surveys by Macro International."
WDIsearch : Data frame with series code, name, description, and source for the WDI series which match the given criteria
Fertility rate:採用The World Bank的各國出生率數據 (註:網址http://data.worldbank.org/indicator/SP.DYN.TFRT.IN)
# Define a list of countries for which to pull data
countries <- c("Australia", "China", "France", "Japan", "South Africa", "United States")
# Convert the country names to iso2c format used in the World Bank data
iso2cNames <- countrycode(countries, "country.name", "iso2c")
iso2cNames
## [1] "AU" "CN" "FR" "JP" "ZA" "US"
找出6個想要研究的國家,此以“Australia”, “China”, “France”, “Japan”, “South Africa”, “United States”為例
用countrycode將名稱做統一的標準化
# Pull data for each countries for the first two fertility rate indicators
wdiData <- WDI(iso2cNames, iMData[1:2, 1], start = 2001, end = 2014)
# Pull out indicator names
iNames <- iMData[1:2, 1]
wdiData,為2001年到2014年出生率的前兩項指標(SP.ADO.TFRT、SP.DYN.TFRT.IN)
# Create trend charts for the first two indicators
for(indicatorName in iNames) {
p <- ggplot(wdiData, aes(x = year, y = wdiData[, indicatorName],
group = country, color = country))+
geom_line(size = 1)+
scale_x_continuous(name = "Year", breaks = c(unique(wdiData[, "year"])))+
scale_y_continuous(name = indicatorName) +
scale_linetype_discrete(name = "Country") +
theme_bw() +
theme(legend.title = element_blank())+
ggtitle(paste(iMData[iMData[,1] == indicatorName, "name"], "n"))
ggsave(paste(indicatorName, ".png", sep=""), p)
}
## Saving 7 x 5 in image
## Saving 7 x 5 in image
利用for迴圈,分別畫出SP.ADO.TFRT和SP.DYN.TFRT.IN在2001到2014各國家生育率的曲線圖 之後將以png的方式存在目前的工作路徑中
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.