Search code examples
rplotggplot2interactionconfidence-interval

Plotting interaction of two continuous variables with confidence bands in ggplot2


I would like to generate an interaction plot with simple slopes and 95% confidence bands for two continuous predictor variables with a continuous outcome variable.

We can use the diamonds data from ggplot2 to address my question. I include syntax to convert the factor variable clarity into a numeric, mean-centered variable so that my question can be answered.

# load package    
library(ggplot2)
# rename data from ggplot2
d <- diamonds

# recode clarity from a factor variable into a numeric variable
levels(d$clarity)
library(plyr)
mapvalues(d$clarity, from = c("I1" ,  "SI2" , "SI1" , "VS2" , "VS1" , "VVS2" ,  "VVS1" , "IF"), 
      to = c("1", "2", "3", "4", "5", "6", "7", "8"))
d$clarity_n <- as.numeric(d$clarity)

I can see the values for the simple slopes in the summary output below. But I can't figure out how to plot them with confidence bands.

# create variables for simple effects
d$carat_MC <- d$carat - mean(d$carat, na.rm=T)
d$clarity_nMC <- d$clarity_n - mean(d$clarity_n, na.rm=T)
d$clarityPLUS_1sd <- d$clarity_nMC + sd(d$clarity_n, na.rm=T)
d$clarityMINUS_1sd <- d$clarity_nMC - sd(d$clarity_n, na.rm=T)

# create a small subset of 500
d <- d[sample(1:nrow(d), 500,replace=FALSE),]

# model the interaction and simple slopes
summary(lm(price~carat_MC*clarity_nMC, data = d))
# simple effect of increased carat for less clear diamonds 
summary(lm(price~carat_MC*clarityPLUS_1sd, data = d))
# simple effect of increased carat for more clear diamonds
summary(lm(price~carat_MC*clarityMINUS_1sd, data = d))

I already know how to create an interaction plot with confidence bands for a factor variable and a continuous variable. If I median split the variable for carat you will see a plot very much like what I want to ultimately get:

# create a new factor variable based on the median split
d$clarity_nMS[ d$clarity_nMC < median(d$clarity_nMC) ] <- -1
d$clarity_nMS[ d$clarity_nMC > median(d$clarity_nMC) ] <- 1
d$clarity_nMS <- as.factor( d$clarity_nMS )

# Begin plotting
ex <- ggplot(d, aes(carat_MC, price, color = clarity_nMS))           

# jitter the scatter plot 
ex <- ex + layer(geom = "point", 
           position = position_jitter(w = 0.1, h = 0.1))

# Add plot lines with confidence intervals. 
ex <- ex + geom_smooth(method="lm", se=TRUE , fullrange=TRUE)  
ex

I would appreciate any assistance for how can I plot an interaction like the one above with simple slopes, 95% confidence bands and, if possible, data points colored by the simple slope they predict for two continuous predictor variables.


Solution

  • As MrFlick suggests, you seem to need a 3D chart and ggplot isn't going to do this for you. In section 13.8 of the R Graphics Cookbook, Winston Chang has a detailed example of how to do a 3D scatter plot with a prediction surface which may be close to what you have in mind. In general, this book is a great reference for R Graphics and ggplot in particular so might be worth acquiring a copy.