Plot multiple variables within a dataframe

Introduction

Sometimes we want to plot multiple variables within a dataframe. In such cases, writing a function and running it for each variable can easily help achieve this goal using ggplot2. Below we show you how to do it step by step.

Setup

First, we load the required R libraries and source the custom functions that we will use throughout this tutorial.

library(readxl)
library(dplyr)
library(tidyr)
library(ggpubr)
library(here)
library(gridExtra)
library(patchwork)
library(ragg)

source(here("utils/theme_and_colors_IMF.R"))

Next we we read the excel data. They are four quarter moving average quarterly data of Panama’s public finances:

fis<-read_excel(here("databases/panama_centralgov.xlsx"), sheet = "FourQma_fiscal")

fis
# A tibble: 40 × 21
    year date                q4_283fgb q4_283fgr q4_283fgrc q4_283fgrt
   <dbl> <dttm>                  <dbl>     <dbl>      <dbl>      <dbl>
 1  2014 2014-03-31 00:00:00       NA        NA         NA         NA 
 2  2014 2014-06-30 00:00:00       NA        NA         NA         NA 
 3  2014 2014-09-30 00:00:00       NA        NA         NA         NA 
 4  2014 2014-12-31 00:00:00    -1945      7098       7042       4999 
 5  2015 2015-03-31 00:00:00    -1693.     7117.      7056.      5018.
 6  2015 2015-06-30 00:00:00    -1170      7152       7092       5081 
 7  2015 2015-09-30 00:00:00    -1657      7283       7222       5177 
 8  2015 2015-12-31 00:00:00    -2010      7268       7206       5072 
 9  2016 2016-03-31 00:00:00    -1899      7516       7458       5293 
10  2016 2016-06-30 00:00:00    -1861      7609       7550       5440 
# ℹ 30 more rows
# ℹ 15 more variables: q4_283frtd <dbl>, q4_283frti <dbl>, q4_283fgrn <dbl>,
#   q4_283frfd <lgl>, q4_283fgrk <dbl>, q4_283frnh <dbl>, q4_283fge <dbl>,
#   q4_283fecx <dbl>, q4_283fepc <dbl>, q4_283fepo <dbl>, q4_283fect <dbl>,
#   q4_283fecd <dbl>, q4_283feph <dbl>, q4_283fgek <dbl>, q4_283fgbc <dbl>

We want to plot several of the variables. Each plot is exactly the same, except for the variable that is used. to avoid unncessary code repetition, it therefore makes sense to write a function.

Writing the Function

We write a function below to call the selected variable var and the date variable in the dataframe fis, then produce a simple line plot for the var using ggplot2.

plotVar<-function(var,title){
  var<-tolower(var)
  fig<-ggplot(fis,aes(x=date,y=0.001*.data[[var]]))+
    geom_line(linewidth=1.7,col=blue)+
    theme_imf_panel()+
    labs(
      title=title,
      x="",
      y=""
    )
  return(fig)
}

Creating the Plots

By calling the function written previously, we can easily plot a batch of charts of various topics for Panama.

fig1<-plotVar("q4_283FGR","Revenues")
fig2<-plotVar("q4_283FGRT","Taxes")
fig3<-plotVar("q4_283FGE", "Expenditure")
fig4<-plotVar("q4_283FECX", "Current expenditure")
fig5<-plotVar("q4_283FECT", "Transfers")
fig6<-plotVar("q4_283FEPO", "Goods and services")
fig7<-plotVar("q4_283FECD", "Interest")
fig8<-plotVar("q4_283FEPC", "Personal services")
fig9<-plotVar("q4_283FGEK", "Capital expenditure")
fig10<-plotVar("q4_283FGB", "Balance")

Combining the Plots

Once all individual plots are created, we can combine them into a single figure layout using patchwork.

patchwork <- 
  (fig1+fig2) /
  (fig3+fig4) /
  (fig5+fig6) /
  (fig7+fig8) /
  (fig9+fig10) 



pw<-patchwork  + plot_annotation(
  title = 'Figure 1. Panama: Fiscal Sector Developments', 
   subtitle = '(Four quarter totals, in billions of US dollars)',
  caption="Sources: Haver.",
  theme = theme(plot.title = element_text(color=blue,family=primary_font,face="bold",
                                          size=20, hjust = 0.5),
                
                plot.subtitle = element_text(color=blue,family=primary_font,face="plain",
                                              hjust=0.5,
                                             size=20),
                plot.caption = element_text(hjust = 0,size=12,family=primary_font,
                ),
                plot.margin = margin(0.75,0.5,0.5,0.5, "lines")
  )) 

pw