-
Notifications
You must be signed in to change notification settings - Fork 23
/
README.Rmd
130 lines (103 loc) · 2.93 KB
/
README.Rmd
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
---
output:
md_document:
variant: markdown_github
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/"
)
```
# wbstats: An R package for searching and downloading data from the World Bank API
<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/wbstats)](https://CRAN.R-project.org/package=wbstats)
[![Monthly](https://cranlogs.r-pkg.org/badges/wbstats)](https://CRAN.R-project.org/package=wbstats)
[![Lifecycle: maturing](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://www.tidyverse.org/lifecycle/#maturing)
<!-- badges: end -->
You can install:
The latest release version from CRAN with
```{r, eval = FALSE}
install.packages("wbstats")
```
or
The latest development version from github with
```{r, eval = FALSE}
remotes::install_github("nset-ornl/wbstats")
```
# Downloading data from the World Bank
```{r readme-download, warning=FALSE}
library(wbstats)
# Population for every country from 1960 until present
d <- wb_data("SP.POP.TOTL")
head(d)
```
## Hans Rosling's Gapminder using `wbstats`
```{r readme-chart, warning=FALSE, message=FALSE, fig.width=12, fig.height=6}
library(tidyverse)
library(wbstats)
my_indicators <- c(
life_exp = "SP.DYN.LE00.IN",
gdp_capita ="NY.GDP.PCAP.CD",
pop = "SP.POP.TOTL"
)
d <- wb_data(my_indicators, start_date = 2016)
d %>%
left_join(wb_countries(), "iso3c") %>%
ggplot() +
geom_point(
aes(
x = gdp_capita,
y = life_exp,
size = pop,
color = region
)
) +
scale_x_continuous(
labels = scales::dollar_format(),
breaks = scales::log_breaks(n = 10)
) +
coord_trans(x = 'log10') +
scale_size_continuous(
labels = scales::number_format(scale = 1/1e6, suffix = "m"),
breaks = seq(1e8,1e9, 2e8),
range = c(1,20)
) +
theme_minimal() +
labs(
title = "An Example of Hans Rosling's Gapminder using wbstats",
x = "GDP per Capita (log scale)",
y = "Life Expectancy at Birth",
size = "Population",
color = NULL,
caption = "Source: World Bank"
)
```
## Using `ggplot2` to map `wbstats` data
```{r ggplot2, fig.height=6, fig.width=8, fig.align="center", message=FALSE, warning=FALSE}
library(rnaturalearth)
library(tidyverse)
library(wbstats)
ind <- "SL.EMP.SELF.ZS"
indicator_info <- filter(wb_cachelist$indicators, indicator_id == ind)
ne_countries(returnclass = "sf") %>%
left_join(
wb_data(
c(self_employed = ind),
mrnev = 1
),
c("iso_a3" = "iso3c")
) %>%
filter(iso_a3 != "ATA") %>% # remove Antarctica
ggplot(aes(fill = self_employed)) +
geom_sf() +
scale_fill_viridis_c(labels = scales::percent_format(scale = 1)) +
theme(legend.position="bottom") +
labs(
title = indicator_info$indicator,
fill = NULL,
caption = paste("Source:", indicator_info$source_org)
)
```