Intro Thoughts

Status Quo

library(tidyverse)

Experiment

library(mosaicData)
library(ggplot2)

model <- lm(totalbill ~ temp + month + temp:month, Utilities)

compute_group_slice = function(data, scales, model, xaxis, facet, n = 100, ...) {

vars <- names(model$model) #Identify col names from model
oldnames <- names(data) #Identify & save names from ggplot data

names(data)[1:3] <- c(xaxis, vars[1], facet) #HARDCODED: Rename ggplot data to match model names
data$month <- as.numeric(data$month) #HARDCODED: Adjust class of facet variable as needed

tmp <- predict(model, newdata = data) #Predict new y col for displaying the model
names(data)<- oldnames #Revert col names to old names
data$y<- tmp

data
}

StatSlice <- ggproto(
"StatSlice",
Stat,
required_aes = c('x','y'),
compute_group = compute_group_slice)

# b <- coef(model)

ggplot(Utilities, 
       aes(x = temp, y = totalbill)) +
  geom_point() +
  facet_wrap(~ month) +
  geom_line(stat = StatSlice, 
            model = model, 
            xaxis = 'temp',
            facet = 'month', 
            color = "skyblue",
            linewidth = 2)

Closing remarks, Other Relevant Work, Caveats