Most economists and other data professionals spend a lot of time creating interesting and insightful data visualisations. Unfortunately, just as many spend perhaps even more time updating their charts or recreating the charts of others.
A key feature of EconData is to allow you to create charts and visualisations that are easily updated or repurposed.
In this blog post, we illustrate how to import data into R using EconData and how to create a simple chart. The idea is to provide a starting point that can be used for automated plotting. The source code can be found here.
Example: Growth in total loans and advances in South Africa (year-on-year)
The first step towards automating a chart is setting up your own template. In your template you should include with the data series that you would like to see on an ongoing basis, and change the plotting instructions to reflect your preferred chart format. Thereafter, all that is required is to run the script any time you would like to see the latest plots of the data.
Please note that the recent update to the new EconDataR package will affect this code, we will update the code soon.
Let’s take it step by step. First, lets load the required R packages.
library(econdatar)
library(dplyr)
library(ggplot2)
library(gridExtra)
Note the inclusion of the EconData package library(econdatar)
. Since this package is available on Github and not yet on CRAN you will not be able to install it in the typical way.
The easiest way to install the EconData R package is by running the following.
install.packages("remotes",
repos = "https://cran.mirror.ac.za")
library("remotes")
install_github("coderaanalytics/econdatar")
Using the read_econdata()
function, we can import the data directly into R. In order to use the function, you will need an active account with EconData, which can be created by following this link.
Note: In order to use this function a username and password is required. This form will be active when the function is run but the window may not pop up. Please check your task bar for the window, to fill in your credentials.
ba100 <- read_econdata(id = "BA100",
key = "TOT..L024",
releasedescription = "Mar 2022")
The function requires, at a minimum, that the data set id be provide – in this case the BA100 data set. We also specify the key so that only the time series that we are interested in be returned. The data we are downloading for this tutorial is total credit extension from all banking institutions in South Africa, which has the data key of TOT.A3.L024 .
Next, let’s take a look at how the data key is constructed for those that are interested. This next section can be skipped without loss of continuity.
The data key refers to the searchable dimensions of the data set. TOT is the first dimension, which represents the bank in question, here specifically it is the aggregation for all the banks in SA – the grand total. A3 is the second dimension, which represents a column, or aggregation, of a table in the BA100 form – here again it is the total. And lastly, L024 is the third dimension, and it represents the 24th line of the BA100 form, namely gross loans and advances. Leaving out any dimension acts as a wildcard for that dimension, for example, the data key TOT..L024 will return all aggregations of the BA100 form matching the two given dimensions. A good way of getting the hang of how this works, is to play with the EconData webapp. Pay specific attention to the export functionality and the generated R code. We will go deeper into the details of the read_econdata()
function in the next post. Help on the function is also available in R using the command ?read_econdata
The data download is accompanied by the meta data associated with the data being explored. The meta data contains useful information about the properties of the data series, such as the date on which the data was released, the unit multiplier, frequency, and unit of measure.
attributes(ba100)$metadata
attributes(ba100$TOT.A3.L024)$metadata
Next, using some functions from the dplyr package, we can convert the time series data to a tibble format which allows for easy data manipulation and plotting. Also using dplyr, we are converting the raw data to a year-on-year growth rate.
loans_and_advances <-
tibble(Period = as.Date(rownames(ba100$TOT.A3.L024)),
Value = ba100$TOT.A3.L024$OBS_VALUE,
Label = "Loans") %>%
mutate(Value = (Value / dplyr::lag(Value, n = 12) - 1) * 100) %>%
filter(!is.na(Value))
Lastly, using the ggplot2 package we can create a simple line plot to visualise the data.
ggplot(data = loans_and_advances) +
geom_line(aes(x = Period, y = Value)) +
labs(caption = "Source: www.econdata.co.za") +
xlab("") + ylab("%") +
theme_classic() +
theme(panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())
The Codera Analytics team
#automation #econdatar #install #plotting #Tutorial
[…] load the required packages. If you missed our previous post on installing the EconData package, here is a link for the installation and account creation […]
[…] As per usual we start by loading the necessary packages. If this is your first time using the EconData R package, you can find the installation instructions here. […]