The example above was created using the following R code in the R visualization script editor:
1234567891011121314151617181920212223242526272829303132333435363738394041
library(lubridate)
library(dplyr)
library(ggplot2)
library(forecast)
## Prepare data
BestRunCorpRetail$`Number of Issues reported` <- BestRunCorpRetail$`Number of Issues reported` / 1000
df <- BestRunCorpRetail %>%
mutate(Year = as.integer(as.character(Year))) %>%
mutate(quarter_num = as.numeric(substr(Quarter, 2, 2))) %>%
mutate(first_month_of_quarter = (quarter_num * 3) -2) %>%
mutate(first_day_of_quarter = make_date(
Year,
first_month_of_quarter,
1)) %>%
select(first_day_of_quarter, `Number of Issues reported`) %>%
arrange(first_day_of_quarter)
## Convert to ts object
first_period = min(df$first_day_of_quarter)
last_period = max(df$first_day_of_quarter)
tso <- ts(df$`Number of Issues reported`,
start = c(year(first_period), quarter(first_period)),
end = c(year(last_period), quarter(last_period)),
frequency = 4)
## Plot
ts <- window(tso,start=2021)
p <- gglagplot(ts, do.lines = TRUE, seasonal=TRUE, colour=TRUE, diag=FALSE , lags = 9) +
labs(title = "Lag plot - Quarterly Number of Issues reported (in k)")
## Layout
p +
theme(
plot.title = element_text(color="red", size=15, face="bold", hjust=0.5 ,margin = margin(t = 0, r = 0, b = 16, l = 0)) ,
panel.border = element_rect(colour = "blue", fill=NA) ,
axis.ticks = element_line( size = 1, linetype = 'solid') ,
axis.ticks.length = unit(8, "pt"),
axis.text.x = element_text(color="black", size = 8, face = "bold" , margin = margin(t = 6, r = 0, b = 0, l = 0)) ,
axis.title.x = element_text(color="blue", size = 12, face = "bold", margin = margin(t = 10, r = 0, b = 0, l = 0)) ,
axis.text.y = element_text(color="red", size = 8, face = "bold" , margin = margin(t = 0, r = 6, b = 0, l = 0)) ,
axis.title.y = element_text(color="yellow", size =12, face = "bold", margin = margin(t = 0, r = 10, b = 0, l = 0)) ,
axis.line = element_line(colour = "black") ,
strip.text = element_text(color="blue", size = 12, face = "bold")
)