Hi @willrm. Your question are little complicate. I found out a way to do it. First you need to get the linear model fitting value in your data frame, not calculate by geom_smooth. Then, fit in each frame of animation that you want to show one by one like what you have done. Hope it can help.
library(ggplot2)
library(tidyverse)
library(gganimate)
year <- as.numeric(c(1996:2002,
1996:2002,
1996:2002))
c <- c(39, 40, 67, 80, 30, 140, 90, 23, 100, 123,
140, 1, 2, 1, 13, 3, 3, 30, 1, 3, 3)
df <- data.frame(year, c) %>%
select(year, c) %>%
arrange(year)
df <- df %>%
group_by(year) %>%
summarise(c = sum(c)) %>%
{mutate(., lm = predict(lm(c ~ year, df), newdata = distinct(df, year)))}
anim <- ggplot(data = df) +
geom_bar(data = df %>% filter(year == 1996), stat="identity", position ="stack",
aes(x = year, y = c)) +
geom_bar(data = df %>% filter(year == 1997), stat="identity", position ="stack",
aes(x = year, y = c)) +
geom_line(filter(df, year %in% c(1996, 1997)), mapping = aes(x = year, y = lm),
colour = "black") +
geom_bar(data = df %>% filter(year == 1998), stat="identity", position ="stack",
aes(x = year, y = c)) +
geom_line(filter(df, year %in% c(1997, 1998)), mapping = aes(x = year, y = lm),
colour = "black") +
geom_bar(data = df %>% filter(year == 1999), stat="identity", position ="stack",
aes(x = year, y = c)) +
geom_line(filter(df, year %in% c(1998, 1999)), mapping = aes(x = year, y = lm),
colour = "black") +
geom_bar(data = df %>% filter(year == 2000), stat="identity", position ="stack",
aes(x = year, y = c)) +
geom_line(filter(df, year %in% c(1999, 2000)), mapping = aes(x = year, y = lm),
colour = "black") +
geom_bar(data = df %>% filter(year == 2001), stat="identity", position ="stack",
aes(x = year, y = c)) +
geom_line(filter(df, year %in% c(2000, 2001)), mapping = aes(x = year, y = lm),
colour = "black") +
geom_bar(data = df %>% filter(year == 2002), stat="identity", position ="stack",
aes(x = year, y = c)) +
geom_line(filter(df, year %in% c(2001, 2002)), mapping = aes(x = year, y = lm),
colour = "black") +
labs(y = "year",
x = "c",
title = "Reprex") +
transition_layers(layer_length = 1, transition_length = 1) +
enter_drift(x_mod = 0, y_mod = -max(df$c))
animate(anim, fps = 10, duration = 10,
width = 600, height = 500, renderer =
gifski_renderer())

Created on 2019-09-27 by the reprex package (v0.3.0)