I want to visualize the results of a linear model where dependent variable values change as a function of discrete x-values. Since my x-values represent consecutive days, I want to annotate the change from day to day, in percents. How can I do this in a line plot?
My Data
I want to measure people's mood. Every day I collect responses from 1000 different people on how they feel. I therefore get a daily average for mood, and I want to see how it changes from one day to another.library(tidyverse)
library(emmeans)
day_1 <- rnorm(1000, mean = 77, sd = 18)
day_2 <- rnorm(1000, mean = 74, sd = 19)
day_3 <- rnorm(1000, mean = 80, sd = 5)
day_4 <- rnorm(1000, mean = 76, sd = 18)
df <-
cbind(day_1, day_2, day_3, day_4) %>%
as.tibble() %>%
gather(., key = day, value = mood, day_1:day_4) %>%
mutate_at(vars(day), factor)
> df
## # A tibble: 4,000 x 2
## day mood
## <fct> <dbl>
## 1 day_1 83.9
## 2 day_1 94.9
## 3 day_1 104.
## 4 day_1 81.0
## 5 day_1 61.4
## 6 day_1 95.1
## 7 day_1 78.6
## 8 day_1 108.
## 9 day_1 74.7
## 10 day_1 79.7
## # ... with 3,990 more rows
Fitting and plotting
fit <- lm(formula = mood ~ day, data = df)
emmip(fit, ~ day, CIs = TRUE)
Given that the plot object can be edited using ggplot functions, how can I add the change between days, in percents, such as the following illustration?
Is there an efficient way to calculate the change and put it above each section of the line?