Hi!
I've trying to plot data that has been mutated into quarterly growth rates from nominal levels.
i.e the original dataset was
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
Which was in an excel file which I have imported using dataset <- read_excel("xx")
Then, I have done the below in order to mutate it to quarter on quarter growth ("QoQ Growth):
dataset %>%
mutate(QoQ Growth= (GDP Level
) / lag(GDP Level
, n=1) - 1)
I would like to now plot this % growth across time, however I'm not too sure how what the geom_line code is for a mutated variable, any help would be really truly appreciated! I'm quite new to R and really trying to learn, thanks!
@nirgrahamuk has a solution for you.
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")…
It would be better if you post the follow-up questions in the same thread.
system
Closed
March 29, 2022, 7:06am
3
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.