Slope Labels on Graph

I am analyzing an interrupted time series. I am having trouble including the value of the two slopes, pre-event and post-event, with the graph of the slopes. Here is the code for the sample data and graph including the slopes. How do I include the value of the two slopes on the graph?

# Clear variables
rm(list=ls())

# Load libraries
library(zoo)
library(tidyverse)
library(fpp2)
library(ggplot2)
library(ggpubr)
library(ggiraph)
library(ggiraphExtra)
library(plyr)
# Create a Sample Dataset#
set. Seed(1)
x <- 100 + arima.sim(model = list(ar = 0.999), n = 104)
Units <- 1.2 * x + rnorm(104)
Units[26:104] <- Units[26:104] + 10
Pre<-1
Pre[1:25]<-0
Pre[26:104]<-1
Pre
Post<-1
Post[1:25]<-0
Post[26:104]<-seq.int(1,79,1)
Post
Week<-seq.Date(as.Date("2019-10-01"),by=7,length.out=104)
reg.data<-data. Frame(Units,Week,Pre,Post)
# Plot data with slopes
#Create the vertical lines marking the stages of intervention
reg.data$group = rep(c("Pre-Intervention","Post-Intervention"), c(25,104-25))
slopeplot=ggplot(reg.data, aes(x = Week, y = Units)) + geom_line() + geom_smooth(method="lm", se=FALSE, aes(colour=group)) +
  ggtitle("Slope Plots for Units Served Pre- and Post-Event with Mean Units") + theme(plot.title = element_text(size = 9))+
  geom_hline(aes(yintercept = mean(Units)), color="darkred", lwd = 0.7, linetype="dashed") +
  theme(legend.position="bottom") + labs(colour="") + ylim(0,200) + geom_vline(xintercept = as.Date(c("2020-03-17")))
slopeplot

via ggpubr add in

  stat_regline_equation(  aes(colour=group)) +

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.