Slope chart

Overview

There is no dedicated chart type for Slope charts. However, with Scalar/Cross sampling analyses you can create slopes out of Category chart.

How to create a Slope chart?

  1. Use Scalar (or Cross sampling) with 'Calculations: Value at' for two (or more) points in time.
  2. Set 'Output series' to 'One series per input'.
  3. Rename 'Value labels'.
  4. Add Category chart.
  5. Add points on each end:
    • click on each line and under Presentation properties > Appearance > Graph style select 'Custom' select 'Marker style',
    • repeat that for all lines.

Cleaning graphical elements

  1. To remove '[scalar]' from legend's descriptions click outside chart area and under Presentation properties >Chart elements untick 'Analysis text'.
  2. Some things cannot be disabled but you can select for them 'transparent color', those are:
    • click on Y-axis, under Presentation properties > Appearance > Text color,
    • click on Y-axis, under Presentation properties > Appearance > Tick color,
    • click on Y-axis' description , under Presentation properties > Appearance > Text color,
    • click on X-axis, under Presentation properties > Appearance > Tick color.
  3. Disable other graphic elements from chart:
    • click on any grid line, under Presentation properties untick 'Show grid lines',
    • click inside chart area, under Presentation properties > Border > Sides select 'Bottom'.

Adding value labels on chart

  1. To add value label right-click on any dot, select 'Add observation label'.
  2. Click on it, under Presentation properties check Custom style:
    • under Anchor line > Width select 'None',
    • under Anchor appearance > Style select 'None'.
  3. Double-click on label's text. Leave only {s .Value}.

Now you can copy the label with Ctrl+c keys, click on any other dot, and paste label with Ctrl+v. It will keep the style of first label.

Example

Slope chart

In this example we calculated annual average CPI and showed its level on years 2019 and 2022.

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)