How to Create an Interaction Plot in R?, To find out if the means of three or more independent groups that have been divided based on two factors differ, a two-way ANOVA is performed.

When we want to determine whether two distinct factors have an impact on a certain response variable, we employ a two-way ANOVA.

The interpretation of the link between the factors and the response variable may be affected, though, when there is an interaction effect between the two factors.

For instance, we might be interested in finding out how gender and exercise affect the response variable weight loss.

While both variables may have an impact on weight loss, it’s also likely that they will work in concert.

For instance, it’s likely that men and women lose weight through exercise at varying rates. Exercise and gender have an interaction effect in this situation.

An interaction plot is the most effective tool for spotting and comprehending the effects of interactions between two variables.

This particular plot style shows the values of the first factor on the x-axis and the fitted values of the response variable on the y-axis.

The values of the second component of interest are depicted by the lines in the plot.

An interaction plot in R can be made and read using the instructions in this tutorial.

## Example: How to Create an Interaction Plot in R

Let’s say researchers want to know if gender and activity volume affect weight loss.

They enlist 30 men and 30 women to take part in an experiment where 10 of each gender are randomly assigned to follow a program of either no activity, light exercise, or severe exercise for one month in order to test this.

To see the interaction impact between exercise and gender, use the following procedures to generate a data frame in R, run a two-way ANOVA, and create an interactive graphic.

### Step 1: Create the data.

The code below demonstrates how to make a data frame in R:

Make this illustration repeatable.

set.seed(123)

Now we can create a data frame

data <- data.frame(gender = rep(c("Male", "Female"), each = 30), Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â exercise = rep(c("None", "Light", "Intense"), each = 10, times = 2), Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â weight_loss = c(runif(10, -3, 3), runif(10, 0, 5), runif(10, 5, 9), Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â runif(10, -4, 2), runif(10, 0, 3), runif(10, 3, 8)))

Letâ€™s view the first six rows of the data frame

head(data)

Â gender exercise weight_loss 1Â Â MaleÂ Â Â Â NoneÂ -1.2745349 2Â Â MaleÂ Â Â Â NoneÂ Â 1.7298308 3Â Â MaleÂ Â Â Â NoneÂ -0.5461385 4Â Â MaleÂ Â Â Â NoneÂ Â 2.2981044 5Â Â MaleÂ Â Â Â NoneÂ Â 2.6428037 6Â Â MaleÂ Â Â Â NoneÂ -2.7266610

### Step 2: Fit the two-way ANOVA model.

How to apply a two-way ANOVA to the data is demonstrated in the code below:

two-way ANOVA model fit

model <- aov(weight_loss ~ gender * exercise, data = data)

Now view the model summary

summary(model)

Df Sum Sq Mean Sq F valueÂ Â Pr(>F)Â Â Â genderÂ Â Â Â Â Â Â Â Â Â 1Â Â 43.7Â Â 43.72Â 19.032 5.83e-05 *** exerciseÂ Â Â Â Â Â Â Â 2Â 438.9Â 219.43Â 95.515Â < 2e-16 *** gender:exerciseÂ 2Â Â Â 2.9Â Â Â 1.46Â Â 0.634Â Â Â 0.535Â Â Â ResidualsÂ Â Â Â Â Â 54Â 124.1Â Â Â 2.30Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â --- Signif. codes:Â 0 â€˜***â€™ 0.001 â€˜**â€™ 0.01 â€˜*â€™ 0.05 â€˜.â€™ 0.1 â€˜ â€™ 1

Note that the p-value (0.535) for the interaction term between exercise and gender is not statistically significant, which indicates that there is no significant interaction effect between the two factors.

### Step 3: Create the interaction plot.

The code below demonstrates how to make an interaction plot for gender and exercise.

interaction.plot(x.factor = data$exercise, #x-axis variable Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â trace.factor = data$gender, #variable for lines Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â response = data$weight_loss, #y-axis variable Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â fun = median, #metric to plot Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ylab = "Weight Loss", Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â xlab = "Exercise Intensity", Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â col = c("pink", "blue"), Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â lty = 1, #line type Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â lwd = 2, #line width Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â trace.label = "Gender")

Interaction plot in R

In general, there is no interaction effect if the two lines on the interaction plot are parallel. However, there will probably be an interaction effect if the lines cross.

As we can see in this plot, there is no intersection between the lines for men and women, suggesting that there is no interaction between the variables of exercise intensity and gender.

This is consistent with the p-value in the ANOVA table’s output, which indicated that the interaction term in the ANOVA model was not statistically significant.