The WorldBank(R) provides open data for hundreds of indicators. These indicators are available for most contries spanning many years. You can learn more at https://data.worldbank.org/indicator?tab=all
The WDI library provides easy access to the data. In this post, I will take a look at the maternal mortality ratio indicator for the United States of America, Brazil, and South Africa.
(The appropriate libraries have been imported. The plotly library will be used for plotting and the dplyr library will be used to extract data for plotting. The WDI library uses the World Bank API to send search queries. A local cache is held for some of the data such as the indicator names. The WDIcache() command can be run to update the cache held for searches.)
The World Bank data that is availabe through this API provides many variable including iso2c country codes, names of the variables, and many more. The WDISearch() command allows strings to be searched for in these variables. In the code block below a search for maternal mortality ratio is made in the name variable.
WDIsearch(string = "maternal mortality ratio",
field = "name",
short = FALSE)
## indicator
## [1,] "SH.STA.MMRT.NE"
## [2,] "SH.STA.MMRT"
## name
## [1,] "Maternal mortality ratio (national estimate, per 100,000 live births)"
## [2,] "Maternal mortality ratio (modeled estimate, per 100,000 live births)"
## description
## [1,] "Maternal mortality ratio is the number of women who die from pregnancy-related causes while pregnant or within 42 days of pregnancy termination per 100,000 live births."
## [2,] "Maternal mortality ratio is the number of women who die from pregnancy-related causes while pregnant or within 42 days of pregnancy termination per 100,000 live births. The data are estimated with a regression model using information on the proportion of maternal deaths among non-AIDS deaths in women ages 15-49, fertility, birth attendants, and GDP."
## sourceDatabase
## [1,] "World Development Indicators"
## [2,] "World Development Indicators"
## sourceOrganization
## [1,] "UNICEF, State of the World's Children, Childinfo, and Demographic and Health Surveys."
## [2,] "WHO, UNICEF, UNFPA, World Bank Group, and the United Nations Population Division. Trends in Maternal Mortality: 1990 to 2015. Geneva, World Health Organization, 2015"
Two indicators are returned. The short = FALSE argument allows for the detailed information.
In the code block below, the WDI() command is used to return a data.frame of the three countries listed for the specified indicator, from 2005 to 2015. The extra = FALSE argument returns a shortened set of variables.
df <- data.frame(
WDI(country = c("US",
"BR",
"ZA"),
indicator = "SH.STA.MMRT",
start = 2005,
end = 2015,
extra = FALSE))
The first six rows can be viewed using the head() command.
head(df)
## iso2c country SH.STA.MMRT year
## 1 BR Brazil 44 2015
## 2 BR Brazil 46 2014
## 3 BR Brazil 48 2013
## 4 BR Brazil 60 2012
## 5 BR Brazil 59 2011
## 6 BR Brazil 65 2010
The full list of variables is shown below.
names(WDI(country = "US",
indicator = "SH.STA.MMRT",
start = 2015,
end = 2016,
extra = TRUE))
## [1] "iso2c" "country" "SH.STA.MMRT" "year" "iso3c"
## [6] "region" "capital" "longitude" "latitude" "income"
## [11] "lending"
In the code block below the filter() command is used to create three data.frame objects, one for each country.
brazil <- df %>% filter(iso2c == "BR")
usa <- df %>% filter(iso2c == "US")
za <- df %>% filter(iso2c == "ZA")
The code block below creates three traces for a single plot using plotly.
trace0 <- brazil$SH.STA.MMRT
trace1 <- usa$SH.STA.MMRT
trace2 <- za$SH.STA.MMRT
dts <- brazil$year
df.plot <- data.frame(dts, trace0, trace1, trace2)
p1 <- plot_ly(df.plot,
x = ~dts,
y = ~trace0,
name = "Brazil",
type = "scatter",
mode = "lines+markers") %>%
add_trace(y = ~trace1,
name = "USA",
type = "scatter",
mode = "lines+markers") %>%
add_trace(y = ~trace2,
name = "RSA",
type = "scatter",
mode = "lines+markers") %>%
layout(title = "Maternal mortality per 100,000 births",
xaxis = list(title = "Year",
zeroline = FALSE),
yaxis = list(title = "Count",
zeroline = FALSE))
p1
The WDI library is extremely easy to use, yet powerful enough to bring a massive open data resource to you desktop. Learn more about it at: https://cran.r-project.org/web/packages/WDI/WDI.pdf
If you would like to know more about maternal mortality rates in South Africa, you can view this issue of the South African Medical Journal: http://www.samj.org.za/index.php/samj/issue/view/215/showToc