Growth Rate graph from nominal data

Hi everyone!

Just getting started with R so quite unfamiliar, I've got a dataset that looks like this:

Date GDP Level
2010Q1 457
2010Q2 487
2010Q3 538
2010Q4 589
2011Q1 627
2011Q2 672.2
2011Q3 716.4
2011Q4 760.6
2012Q1 804.8
2012Q2 849
2012Q3 893.2
2012Q4 937.4

Essentially, I have been able to plot each year on the x and each corresponding level of GDP on the y

However, I was wondering how to plot:

quarter to quarter % growth: i.e. gdp growth rate for (2012Q3-2012Q2)/2012Q2

year on year % growth: i.e. gdp growth rate for (2012Q3-2011Q3)/2011Q3

just not really sure how to ggplot % changes, really trying to learn R but finding it hard, any help would be greatly appreciated!

library(tidyverse)
# example data
(df_0 <- data.frame(
  stringsAsFactors = FALSE,
  quarters = c("a","b","c","d","e","f",
               "g","h","i","j","k","l","m","n","o","p","q","r",
               "s","t","u","v","w","x","y","z"),
  val = c(50,116,142,217,318,337,
          387,435,460,532,633,723,761,782,809,813,855,945,
          973,1010,1106,1112,1197,1232,1325,1329)
))

#calculate changes
(df_1 <- mutate(df_0,
                qtq = val/lag(val,n=1), # lag is to get a value n entries previous
                yoy = val/lag(val,n=4))
                )

#plot them

ggplot(data=df_1) +
  aes(x=quarters,group=1) + 
  geom_line(aes(y=qtq,color="qtq")) + 
  geom_line(aes(y=yoy,color="yoy")) +
  scale_color_manual(values = c("black","blue"))+
  scale_y_continuous(labels = scales::percent_format())

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.