# How to Create an Interaction Plot in R?

**Data Science Tutorials**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post How to Create an Interaction Plot in R? appeared first on Data Science Tutorials

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.

The post How to Create an Interaction Plot in R? appeared first on Data Science Tutorials

**leave a comment**for the author, please follow the link and comment on their blog:

**Data Science Tutorials**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.