You can explore all the economic data from different providers by following the link db.nomics.world
(N.B. : in the examples, data have already been retrieved on january 5th 2019).
idsFirst, let’s assume that we know which series we want to download. A series identifier (ids) is defined by three values, formatted like this: provider_code/dataset_code/series_code.
library(magrittr)
library(dplyr)
library(ggplot2)
library(rdbnomics)
df <- rdb(ids = 'AMECO/ZUTN/EA19.1.0.0.0.ZUTN') %>%
filter(!is.na(value))
In such data.frame, you will always find at least nine columns:
provider_codedataset_codedataset_nameseries_codeseries_nameoriginal_period (character string)period (date of the first day of original_period)value@frequency (harmonized frequency generated by DBnomics)The other columns depend on the provider and on the dataset. They always come in pairs (for the code and the name). In the data.frame df, you have:
unit (code) and Unit (name)geo (code) and Country (name)freq (code) and Frequency (name)ggplot(df, aes(x = period, y = value, color = series_code)) +
geom_line(size = 2) +
dbnomics()
df <- rdb(ids = c('AMECO/ZUTN/EA19.1.0.0.0.ZUTN', 'AMECO/ZUTN/DNK.1.0.0.0.ZUTN')) %>%
filter(!is.na(value))
ggplot(df, aes(x = period, y = value, color = series_code)) +
geom_line(size = 2) +
dbnomics()
df <- rdb(ids = c('AMECO/ZUTN/EA19.1.0.0.0.ZUTN', 'Eurostat/une_rt_q/Q.SA.TOTAL.PC_ACT.T.EA19')) %>%
filter(!is.na(value))
ggplot(df, aes(x = period, y = value, color = series_code)) +
geom_line(size = 2) +
dbnomics()
maskThe code mask notation is a very concise way to select one or many time series at once. It is compatible only with some providers : BIS, ECB, Eurostat, FED, ILO, IMF, INSEE, OECD, WTO.
df <- rdb('IMF', 'CPI', mask = 'M.DE.PCPIEC_WT') %>%
filter(!is.na(value))
ggplot(df, aes(x = period, y = value, color = series_code)) +
geom_step(size = 2) +
dbnomics()
You just have to add a + between two different values of a dimension.
df <- rdb('IMF', 'CPI', mask = 'M.DE+FR.PCPIEC_WT') %>%
filter(!is.na(value))
ggplot(df, aes(x = period, y = value, color = series_code)) +
geom_step(size = 2) +
dbnomics()
df <- rdb('IMF', 'CPI', mask = 'M..PCPIEC_WT') %>%
filter(!is.na(value)) %>%
arrange(desc(period), REF_AREA) %>%
head(100)
df <- rdb('IMF', 'CPI', mask = 'M..PCPIEC_IX+PCPIA_IX') %>%
filter(!is.na(value)) %>%
group_by(INDICATOR) %>%
top_n(n = 50, wt = period)
dimensionsSearching by dimension is a less concise way to select time series than using the code mask, but it works with all the different providers. You have a “Description of series code” at the bottom of each dataset page on the DBnomics website.
df <- rdb('AMECO', 'ZUTN', dimensions = list(geo = "ea19")) %>%
filter(!is.na(value))
# or
# df <- rdb('AMECO', 'ZUTN', dimensions = '{"geo": ["ea19"]}') %>%
# filter(!is.na(value))
ggplot(df, aes(x = period, y = value, color = series_code)) +
geom_line(size = 2) +
dbnomics()
df <- rdb('AMECO', 'ZUTN', dimensions = list(geo = c("ea19", "dnk"))) %>%
filter(!is.na(value))
# or
# df <- rdb('AMECO', 'ZUTN', dimensions = '{"geo": ["ea19", "dnk"]}') %>%
# filter(!is.na(value))
ggplot(df, aes(x = period, y = value, color = series_code)) +
geom_line(size = 2) +
dbnomics()
df <- rdb('WB', 'DB', dimensions = list(country = c("DZ", "PE"), indicator = c("ENF.CONT.COEN.COST.ZS", "IC.REG.COST.PC.FE.ZS"))) %>%
filter(!is.na(value))
# or
# df <- rdb('WB', 'DB', dimensions = '{"country": ["DZ", "PE"], "indicator": ["ENF.CONT.COEN.COST.ZS", "IC.REG.COST.PC.FE.ZS"]}') %>%
# filter(!is.na(value))
ggplot(df, aes(x = period, y = value, color = series_name)) +
geom_line(size = 2) +
dbnomics()
When you don’t know the codes of the dimensions, provider, dataset or series, you can:
go to the page of a dataset on DBnomics website, for example Doing Business,
select some dimensions by using the input widgets of the left column,
click on “Copy API link” in the menu of the “Download” button,
use the rdb_by_api_link function such as below.
df <- rdb_by_api_link("https://api.db.nomics.world/v22/series/WB/DB?dimensions=%7B%22country%22%3A%5B%22FR%22%2C%22IT%22%2C%22ES%22%5D%7D&q=IC.REG.PROC.FE.NO&observations=1&format=json&align_periods=1&offset=0&facets=0") %>%
filter(!is.na(value))
ggplot(df, aes(x = period, y = value, color = series_name)) +
geom_step(size = 2) +
dbnomics()
rdb_by_api_link function. Please note that when you update your cart, you have to copy this link again, because the link itself contains the ids of the series in the cart.
df <- rdb_by_api_link("https://api.db.nomics.world/v22/series?series_ids=BOE%2F8745%2FLPMB23A%2CBOE%2F8745%2FLPMB26A&observations=1&format=json&align_periods=1") %>%
filter(!is.na(value))
ggplot(df, aes(x = period, y = value, color = series_name)) +
geom_line(size = 2) +
scale_y_continuous(labels = function(x) { format(x, big.mark = " ") }) +
dbnomics()
Could not resolve hostWhen using the functions rdb or rdb_by_api_link, you may come across the following error :
Error in open.connection(con, "rb") :
Could not resolve host: api.db.nomics.world
This error is due to a fail of the function fromJSON. To get round this situation, you can use the readLines function before the fromJSON function by setting an option of the package :
options(rdbnomics.use_readLines = TRUE)
df1 <- rdb(ids = 'AMECO/ZUTN/EA19.1.0.0.0.ZUTN')
df2 <- rdb(ids = c('AMECO/ZUTN/EA19.1.0.0.0.ZUTN', 'AMECO/ZUTN/DNK.1.0.0.0.ZUTN'))
or by using the argument use_readLines of the function rdb_by_api_link :
df1 <- rdb(ids = 'AMECO/ZUTN/EA19.1.0.0.0.ZUTN', use_readLines = TRUE)
df2 <- rdb(
ids = c('AMECO/ZUTN/EA19.1.0.0.0.ZUTN', 'AMECO/ZUTN/DNK.1.0.0.0.ZUTN'),
use_readLines = TRUE
)
dbnomics() used in the vignetteWe show the function dbnomics() as an information. It’s not implemented in the package.
dbnomics <- function(legend_title = "Code") {
list(
scale_x_date(expand = c(0, 0)),
xlab(""),
ylab(""),
guides(color = guide_legend(title = legend_title)),
theme_bw(),
theme(
legend.position = "bottom", legend.direction = "vertical",
legend.background = element_rect(fill = "transparent", colour = NA),
legend.key = element_blank(),
panel.background = element_rect(fill = "transparent", colour = NA),
plot.background = element_rect(fill = "transparent", colour = NA),
legend.title = element_blank()
),
annotate(
geom = "text", label = "DBnomics",
x = structure(Inf, class = "Date"), y = -Inf,
hjust = 1.1, vjust = -0.4, col = "grey",
fontface = "italic"
)
)
}