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())