Interaction Plot in R: How to Visualize Interaction Effect Between Variables

Estimated time:
time
min

By far the easiest way to detect and interpret the interaction between two-factor variables is by drawing an interaction plot in R. It displays the fitted values of the response variable on the Y-axis and the values of the first factor on the X-axis. The second factor is represented through lines on the chart - each possible value of the second factor gets its own line.

Today you’ll learn when you should consider plotting interaction charts and how you can do it in R.
<blockquote>Nervous about your upcoming data science interview? Make sure you can answer these <a href="https://appsilon.com/data-science-coding-interview-questions/" target="_blank" rel="noopener noreferrer">7 technical questions</a>.</blockquote>
Table of contents:
<ul><li><a href="#dataset">Dataset Preparation for Interaction Plots</a></li><li><a href="#anova">Quantify Relationships Using ANOVA</a></li><li><a href="#visualization">Visualizing Interaction Plot in R</a></li><li><a href="#conclusion">Conclusion</a></li></ul>

<hr />

<h2 id="dataset">Dataset Preparation for Interaction Plots</h2>
The first order of business is to acquire a dataset. The best ones a typically those produced by statistical research. For example, this <a href="https://www.sheffield.ac.uk/polopoly_fs/1.570199!/file/stcp-Rdataset-Diet.csv" target="_blank" rel="noopener noreferrer">diet dataset</a> contains information on people who undertook one of three diets - everything from their age, gender, height, diet regime, and weight before and after the six-week period.

Download the dataset in a CSV format and use the following code snippet to load it into R:

<script src="https://gist.github.com/darioappsilon/15767a023b8d89fc609f381951621cd1.js"></script>

Here’s what the first six rows look like:

<img class="size-full wp-image-11794" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01ec34d7961f8266fceca_head-of-the-diet-dataset.webp" alt="Image 1 - Head of the Diet dataset" width="1204" height="358" /> Image 1 - Head of the Diet dataset

Now, we don’t need everything from the dataset. All we care about is the relationship of weight loss in consideration with two factors - gender and diet regime.

The code snippet below transforms the dataset so that missing values are removed, weight loss is calculated, variables are converted to factors, and only columns of interest are kept:

<script src="https://gist.github.com/darioappsilon/c83d68ba9e50a570a9935e4b31e82e8b.js"></script>

We have a much leaner dataset now:

<img class="size-full wp-image-11796" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01ec4369f5360b2ad67e8_head-of-the-tranformed-dataset.webp" alt="Image 2 - Head of the transformed Diet dataset" width="672" height="354" /> Image 2 - Head of the transformed Diet dataset

That’s all we need to visualize the interaction plot in R. But before we do so, we need to know is there a reason to even consider the interaction plot. That’s a question ANOVA test can answer.
<h2 id="anova">Quantify Relationships Using ANOVA</h2>
We can do a two-way ANOVA test to find out if two factors affect the response variable. We've covered ANOVA on the Appsilon blog, and shown how to implement it from scratch. Consider these resources if you want to learn more:
<ul><li><a href="https://appsilon.com/anova-in-r/">ANOVA in R - How to Implement One-Way ANOVA From Scratch</a></li><li><a href="https://appsilon.com/manova-in-r/">MANOVA in R - Multivariate ANOVA explained in R</a></li></ul>
Back to the topic. Our two-way ANOVA model should explain the effect of diet and gender factors on the response variable - weight loss. Use the following code snippet to fit the model and print its summary:

<script src="https://gist.github.com/darioappsilon/12835db1046270d63ed2e256564faa8f.js"></script>

Here’s the model summary:

<img class="size-full wp-image-11792" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01ec6cd7330b395c69286_ANOVA-model-results.webp" alt="Image 3 - ANOVA model results" width="1358" height="362" /> Image 3 - ANOVA model results

The only thing we’re looking at here is the P-value of <code>diet.type:gender</code>. It’s below the significance level (0.05), which indicates there’s a significant interaction effect between the factors.

Knowing this, the next logical step is to visualize the interaction plot.
<h2 id="visualization">Visualizing Interaction Plot in R</h2>
R has the <code>interaction.plot()</code> function built-in, but it comes with a ton of parameters beginners can find confusing. The following list explains all the parameters you need to create an interaction plot:
<ul><li><code>x.factor</code> - A factor variable whose levels will be on the X-axis.</li><li><code>trace.factor</code> - The second-factor variable whose levels will be represented as traces (lines).</li><li><code>response</code> - A numeric response variable.</li><li><code>fun</code> - The function to compute the summary, e.g. median.</li><li><code>ylab</code> - Y-axis label of the plot.</li><li><code>xlab</code> - X-axis label of the plot.</li><li><code>trace.label</code> - Label for the legend.</li><li><code>col</code> - A vector of colors used for all traces.</li><li><code>lyt</code> - A type of the lines drawn.</li><li><code>lwd</code> - Width of the lines drawn.</li></ul>
It’s a lot, but none of it should feel difficult to understand. Let’s now use the function to draw the interaction plot:

<script src="https://gist.github.com/darioappsilon/f889091c1267f64e22dcbe76803f414c.js"></script>

Here’s what it looks like:

<img class="size-full wp-image-11800" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01ec6feabe8f16c325c60_interaction-plot-of-weightloss-and-diet-type-per-gender.webp" alt="Image 4 - Interaction plot of weight loss and diet type per gender" width="2272" height="1538" /> Image 4 - Interaction plot of weight loss and diet type per gender

If the lines on the interaction plot are parallel, then there’s no interaction between the factors. If the lines intersect, then there’s likely an interaction between them.

We can see that our lines are intersecting, which means there’s an interaction between diet type, gender, and weight loss. The results are expected, as the P-value from the ANOVA test told us there’s a significant interaction effect between them.

<hr />

<h2 id="conclusion">Summing Up Interaction Plots in R</h2>
Today you’ve learned what an interaction plot in R is and how it can help you. It’s an excellent supplement to ANOVA tests and allows you to replace tables of numbers with easily interpretable data visualization.

For a homework assignment, we recommend you apply the same code to your dataset or a dataset based on another statistical research. Share your results with us on Twitter – <a href="https://twitter.com/appsilon" target="_blank" rel="noopener noreferrer">@appsilon</a>. We’d love to see what you come up with.
<blockquote>Want to dive further into visualizing line charts? Follow our <a href="https://appsilon.com/ggplot2-line-charts/" target="_blank" rel="noopener noreferrer">complete guide to stunning visuals with ggplot2</a>.</blockquote>

Contact us!
Damian's Avatar
Damian Rodziewicz
Head of Sales
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
r
data visualization
tutorials