Utility Scripts

Why Do We Have Utility Scripts?

The R book relies on a set of utility scripts to improve efficiency, ensure consistency, and enhance reproducibility in data analysis and visualization. Instead of writing repetitive code, we modularize key functions into dedicated scripts that you can load and utilize.

These utilities help IMF users ensure reports and figures adhere to institutional standards, and external users to implement these practices in their own work.

List of Utility Scripts

Below is a list of the key utility scripts included in this book and their purpose.

File Name Purpose
Add_text_to_figure_panel.R Adds narratives to individual charts made using ggplot2.
Add_text_to_figure_panel_base_R.R Adds narratives to individual charts made using base R.
fetchHaver.R Retrieves time-series data from Haver Analytics.
imf_data_utils.R Provides functions for retrieving and processing IMF data via iData.
theme_and_colors_IMF.R Defines IMF color schemes and themes for ggplot2 charts.
theme_and_colors_IMF_base_R.R Defines IMF color schemes and themes for Base R charts.

Standardizing Text Annotations in Figures

There are two key scripts that add narratives to individual charts in a standardized manner, both ggplot2 and baseR figures.

Add_text_to_figure_panel.R (ggplot2 version)

This script adds narratives to individual charts within a panel chart specifically in ggplot2, ensuring that figures have consistent, structured text annotations.

Why It’s Important

  • Ensures titles, captions, and annotations are consistently formatted.

  • Standardizes text placement across multiple charts in a panel.

An example of this is shown in the section,Panel charts with text annotations, in the ggplot chapter.

Add_text_to_figure_panel_base_R.R (Base R version)

This script is similar to the one above, but is specific to base R charts. It adds narratives to individual charts in base R using using mtext().

Why It’s Important

  • Ensures consistency between base R and ggplot2 visualizations.

  • Provides an easy way to annotate standard plots.

An example of this is shown in the Panel charts with text annotations in the Base R chapter of the book.

Fetching and Handling IMF & Haver Data

We have also developed scripts to automate the retrieval of commonly used data sources, like Haver or iData.

fetchHaver.R (Fetching Haver Analytics Data)

This script automates fetching data from Haver Analytics, providing cleaner time-series data, and standardizing the merging and formatting of this data.

For example, the function fetchHaver(varcode, database, frequency="daily") retrieves data from Haver and formats it as an xts object, ready for easy plotting.

imf_data_utils.R (Fetching IMF Data)

This script allows us to access iData, and handles IMF database queries, authentication, and processing. This allows us to retrieve IMF data with an automated authentication, converts raw IMF time-series data into a structured format, and processes quarterly, monthly, and annual frequency data. For example, the function imfdata_by_key(department, dataset, key, needs_auth = FALSE, needs_labels = FALSE) provides the user with an easy way to fetch time-series data from the IMF’s API.

The full tutorial can be found under the accessing iData tutorial.

Standardizing Colors and Themes

We have developed two key scripts to define and utilize the IMF-approved color palettes and themes both in ggplot and base R.

theme_and_colors_IMF.R (ggplot2 version)

This script defines the IMF-approved color palettes and themes specifically for ggplot charts. This ensures the charts subject to this theme are in line with the IMF Article IV branding, and simplifies their application to a chart.

  • theme_imf() → Applies IMF styling to regular plots.

  • theme_imf_panel() → Adjusts formatting for panel charts.

theme_and_colors_IMF_base_R.R (Base R version)

This script defines color palettes and a theme function for base R plots. It also ensures base R plots match the ggplot formatting so that both apply consistent color schemes and formatting.

  • set_imf_theme() → Applies IMF styling to Base R plots.

These are the key utility scripts used in this Book. You can also build your own utility scripts to streamline repetitive tasks, customize data processing workflows, and enhance the readability and consistency of your analysis and visualizations.