Slope chart (incl. vintage/revision data)

Macrobond R API is available for users with Data+ and Legacy license (the latter without searching and revision history functions). Examples were tested on Macrobond version 1.27, Macrobond API for R version 1.2-8 and R 4.3.0.

See two methods of constructing slope charts - with CGPfunctions and ggplot2. Example is presented on IMF’s annual CPI data. Below them you will find example using vintage data (revision history) on DG ECFIN's CPI estimate data.

 

Slope chart version 1 created with CGPfunctions package (click to enlarge). Jump to the code with CGPfunctions.

 

 

 

 

Slope chart version 2 created with ggplot2 + ggrepel packages (click to enlarge). Jump to the code with ggplot2.

 

 

 

 

Slope chart version 3 created with CGPfunctions package (click to enlarge). It uses vintage (revision history) data. Jump to the code.

 

With CGPfunctions package

library(MacrobondAPI)
library(countrycode)
library(tidyverse)
library(CGPfunctions)

#download series
list_of_series <- c("imfcpi_132a_pcpiha_ix", "imfcpi_134a_pcpiha_ix", "imfcpi_136a_pcpiha_ix", "imfcpi_935a_pcpiha_ix", "imfcpi_936a_pcpiha_ix", "imfcpi_944a_pcpiha_ix", "imfcpi_964a_pcpiha_ix", "imfcpi_968a_pcpiha_ix")

#create empty vectors
values_1<-NULL
values_2<-NULL
country<-NULL

#get first values from a specific date
for (i in list_of_series){
  values_1<-append(values_1, getValueAtDate(FetchOneTimeSeries(i), as.Date("2019-01-01")))
}

#get second values 
for (i in list_of_series){
  values_2<-append(values_2, last(getValues(FetchOneTimeSeries(i))))
}

#get country name
for (i in list_of_series){
  country<-append(country, getMetadataValues(getMetadata(FetchOneTimeSeries(i)), "Region"))
}
#change ISO codes to names
country<-countrycode(as.vector(country), origin="iso2c", destination ="country.name")

#prepare data frame
data <- data.frame(values_1, values_2, country) 
colnames(data) <- c("Year2019","Year2021","country") #set column names specific for this data set
data <- mutate(data, Year2019=round(Year2019, 1), Year2021=round(Year2021, 1)) #round up values 
data <- gather(data, key="year", value="value", Year2019:Year2021) #restructure data for the slope graph

#prepare colors
colors <- c("Hungary" = "#F68D2E", "Czechia" = "gray",
          "Romania" = "gray", "Slovakia" = "gray",
          "Germany" = "gray", "France" ="gray",
          "Poland"="#CD545B", "Euro Area" = "blue", 
          "Italy" = "gray")

#chart
newggslopegraph(dataframe = data,
                Times = year,
                Measurement = value,
                Grouping = country,
                Title = "Consumer Price Index",
                SubTitle = "Harmonized, All Items",
                Caption = "Source: IMF",
                LineThickness = 1.5,
                LineColor = colors,
                XTextSize = 8, #size of the column title
                YTextSize = 3.2, #size of the country name
                DataTextSize = 4.2, #size of the value
                TitleTextSize = 17,
                SubTitleTextSize = 10,
                CaptionTextSize = 8)

With ggplot2 + ggrepel packages

library(MacrobondAPI)
library(countrycode)
library(tidyverse)
library(ggplot2)
library(ggrepel)

#download series
list_of_series <- c("imfcpi_132a_pcpiha_ix", "imfcpi_134a_pcpiha_ix", "imfcpi_136a_pcpiha_ix", "imfcpi_935a_pcpiha_ix","imfcpi_936a_pcpiha_ix", "imfcpi_944a_pcpiha_ix", "imfcpi_964a_pcpiha_ix", "imfcpi_968a_pcpiha_ix")

#create empty vectors
values_1<-NULL
values_2<-NULL
country<-NULL

#get first values from a specific date
for (i in list_of_series){
  values_1<-append(values_1, getValueAtDate(FetchOneTimeSeries(i), as.Date("2019-01-01")))
}

#get second values 
for (i in list_of_series){
  values_2<-append(values_2, last(getValues(FetchOneTimeSeries(i))))
}

#get country name
for (i in list_of_series){
  country<-append(country, getMetadataValues(getMetadata(FetchOneTimeSeries(i)), "Region"))
}
#change ISO codes to names
country<-countrycode(as.vector(country), origin="iso2c", destination ="country.name")

#prepare data frame
data <- data.frame(values_1, values_2, country)
colnames(data) <- c("Year2019","Year2021","country") #set column names specific for this data set
data <- mutate(data, Year2019=round(Year2019, 1), Year2021=round(Year2021, 1)) #round up values 
data <- gather(data, key="year", value="value", Year2019:Year2021) 

#chart 
ggplot(data = data, aes(x = year, y = value, group = country)) + 
geom_line(aes(color = country, alpha = 1), size = 1) + 
geom_point(aes(color = country, alpha = 1), size = 3) + 
geom_text_repel(data = data %>% filter(year == "Year2019"), #filter data to apply settings only to the left group
            aes(label=paste0(country, " - ", value)),
            hjust = 1.4, size=2.5, fontface = "bold")+
  geom_text_repel(data = data %>% filter(year == "Year2021"), #filter data to apply settings only to the right group
            aes(label=paste0(value, " - ", country)),
            hjust = -0.4, size=2.5, fontface = "bold")+
  labs(title = "Consumer Price Index",
       subtitle = "Harmonized, All Items",
       caption = "Source: IMF")+
  theme(legend.position = "none", #cleaning graphic elements
        panel.border = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        panel.grid.major.y = element_blank(),
        panel.grid.minor.y = element_blank(),
        axis.title.x = element_blank(),
        panel.grid.major.x = element_blank(),
        axis.ticks = element_blank(),
        plot.title = element_text(size=14, face = "bold"),
        plot.subtitle = element_text(size=8),
        plot.caption = element_text(size=8))+
  scale_x_discrete(position = "top")

With vintage data (revision history)

library(MacrobondAPI)
library(countrycode)
library(tidyverse)
library(CGPfunctions)

#download series
list_of_series <- c("ameco_bgr_1_0_0_0_zcpih", "ameco_hun_1_0_0_0_zcpih","ameco_pol_1_0_0_0_zcpih", "ameco_rom_1_0_0_0_zcpih")

#create empty vectors
values_1<-NULL
values_2<-NULL
country<-NULL

#check vintage times, when series was updated?
getVintageTimes(FetchOneTimeSeriesWithRevisions("ameco_deu_1_0_0_0_zcpih"))

#get value from vintage series for specified date
for (i in list_of_series){
  values_1<-append(values_1, last(getValues(getVintage(FetchOneTimeSeriesWithRevisions(i), as.Date("2021-12-01")))))
}

#get last value from current series 
for (i in list_of_series){
  values_2<-append(values_2, last(getValues(FetchOneTimeSeries(i))))
}

#get country name
for (i in list_of_series){
  country<-append(country, getMetadataValues(getMetadata(FetchOneTimeSeries(i)), "Region"))
}
#change ISO codes to names
country<-countrycode(as.vector(country), origin="iso2c", destination ="country.name")

#prepare data frame
data <- data.frame(round(values_1, 1), round(values_2, 1), country) #round up series in one move
colnames(data) <- c("2021 q4","2022 q1","country") #set column names specific for this data set
data <- gather(data, key="year", value="value", "2021 q4":"2022 q1") #restructure data for the slope graph

#prepare colors
colors <- c("Bulgaria" = "#F68D2E","Hungary" = "gray", 
            "Poland" = "#CD545B", "Romania"="gray")

#chart
newggslopegraph(dataframe = data,
                Times = year,
                Measurement = value,
                Grouping = country,
                Title = "CPI, estimate for 2023",
                SubTitle = "From 2021 Q4 and 2022 Q2",
                Caption = "Source: DG ECFIN",
                LineThickness = 1.5,
                LineColor = colors,
                XTextSize = 7, #size of the column title
                YTextSize = 3.2, #size of the country name
                DataTextSize = 4.2, #size of the value
                TitleTextSize = 17,
                SubTitleTextSize = 10,
                CaptionTextSize = 8)