# How to Create an Interaction Plot in R

An interaction plot is a visual representation that allows you to understand the interaction effects between two or more categorical variables on a continuous dependent variable. It is a line graph that depicts the relationship between the dependent variable and one independent variable for various categories (levels) of the other independent variable.

In the context of statistical modeling, an interaction effect occurs when the effect of one independent variable on the dependent variable depends on the level of another independent variable. In other words, the interaction between variables happens when the impact of one variable changes according to the level of another variable.

In R, several packages and functions can help you create an interaction plot. This article will discuss three methods: the base R function interaction.plot(), the effects package, and the ggplot2 package.

## Creating Interaction Plots in Base R

R provides a base function, interaction.plot(), that can be used for creating interaction plots. This function represents the interaction of two factors by plotting the means of one factor against the levels of the other factor. The graph uses different line types to distinguish the levels of the first factor.

Here is an example of how to create an interaction plot using the interaction.plot() function in R.

First, let’s create a simple dataset:

# Create a dataset
set.seed(123)
factor1 <- as.factor(rep(c("A", "B"), each = 50))
factor2 <- as.factor(rep(c("X", "Y"), 50))
response <- rnorm(100, mean = rep(c(2, 3), each = 50), sd = 0.5)
df <- data.frame(factor1, factor2, response)

In the dataset, factor1 and factor2 are the two categorical independent variables, and response is the continuous dependent variable.

Next, let’s create the interaction plot:

# Create an interaction plot
with(df, interaction.plot(factor2, factor1, response,
xlab="Factor 2",
ylab="Mean of Response",
trace.label="Factor 1",
legend=T))

The interaction.plot() function takes as arguments the x-factor, trace-factor, and response vector. It also has arguments for the x and y labels, the label for the trace factor, and whether to display a legend.

## Creating Interaction Plots using the effects package

The effects package provides functions for graphical and tabular effect displays. The Effect() function computes the effect for a term (or a list of terms) in a linear or generalized linear model. The plot() function is used to create the interaction plot.

Here is how to create an interaction plot using the effects package:

First, install and load the package:

# Install
install.packages("effects")

library(effects)

Next, fit a linear model to the data:

# Fit a model
model <- lm(response ~ factor1*factor2, data = df)

In the model, factor1*factor2 indicates that we are considering both the main effects of factor1 and factor2, as well as their interaction.

Finally, use the Effect() and plot() functions to create the interaction plot:

# Create an interaction plot
effect_plot <- Effect(c("factor1", "factor2"), model)
plot(effect_plot)

The Effect() function computes the effect of factor1 and factor2 (and their interaction) on the response, based on the fitted model. The plot() function then creates the interaction plot.

## Creating Interaction Plots using the ggplot2 package

The ggplot2 package is a powerful and flexible package for creating plots in R. You can also use it to create interaction plots.

First, install and load the package:

# Install
install.packages("ggplot2")

library(ggplot2)

Next, create the interaction plot:

# Create an interaction plot
ggplot(df, aes(x=factor2, y=response, color=factor1)) +
stat_summary(fun=mean, geom="point") +
stat_summary(fun=mean, geom="line", aes(group=factor1)) +
labs(x="Factor 2", y="Mean of Response", color="Factor 1")

In the ggplot() function, the aes() function is used to map variables in the data to visual properties of the plot. The stat_summary() function is used to summarize the response variable by the mean, and the geom="point" and geom="line" arguments are used to add points and lines to the plot.

## Conclusion

Interaction plots are useful for understanding the interaction effects between two or more categorical variables on a continuous dependent variable. In R, you can create interaction plots using the base function interaction.plot(), or you can use the effects or ggplot2 packages. Each method has its strengths: the base R function is simple and straightforward, the effects package allows for effect displays based on fitted models, and the ggplot2 package offers powerful and flexible plotting capabilities. Choose the method that best fits your needs for creating interaction plots in R.

Posted in RTagged