Link Search Menu Expand Document (external link)

How to plot interaction effects of treatments

Description

When there are multiple treatment conditions with multiple levels and you wish to undertsand the interaction effects of each of them, a plot can be useful. How can we create the right kind of plot for that situation?

Using Matplotlib and Seaborn, in Python

View this solution alone.

The solution below uses an example dataset about the teeth of 10 guinea pigs at three Vitamin C dosage levels (in mg) with two delivery methods (orange juice vs. ascorbic acid). (See how to quickly load some sample data.)

1
2
from rdatasets import data
df = data('ToothGrowth')

To plot the interaction effects among tooth length, supplement, and dosage, we can use the pointplot function in the Seaborn package.

1
2
3
4
5
import seaborn as sns
import matplotlib.pyplot as plt
sns.pointplot(x='dose',y='len',hue='supp',data=df)
plt.legend(loc='lower right')  # Default is upper right, which overlaps the data here.
plt.show()

png

Looking at the output, we first see that there is an interaction effect because the two supp lines intersect. We also see that there is a difference in length when giving 0.5mg and 1mg dosage of either of the two delivery methods. However, there is barely any difference between the delivery methods when the dosage level is 2mg.

Content last modified on 24 July 2023.

See a problem? Tell us or edit the source.

Using ggpubr, in R

View this solution alone.

The solution below uses an example dataset about the teeth of 10 guinea pigs at three Vitamin C dosage levels (in mg) with two delivery methods (orange juice vs. ascorbic acid). (See how to quickly load some sample data.)

1
df <- ToothGrowth

To plot the interaction effects among tooth length, supplement, and dosage, we can use the ggline function in the ggpubr package. You can change the x and color inputs below depending on your goals, but the y input should always be the dependent variable.

1
2
3
# install.packages("ggpubr") # If you have not already installed it
library(ggpubr)
ggline(df, x="dose", y="len", color="supp", add=c("mean"))
1
Loading required package: ggplot2

Looking at the output, we first see that there is an interaction effect because the two supp lines intersect. We also see that there is a difference in length when giving 0.5mg and 1mg dosage of either of the two delivery methods. However, there is barely any difference between the delivery methods when the dosage level is 2mg.

Content last modified on 24 July 2023.

See a problem? Tell us or edit the source.

Topics that include this task

Opportunities

This website does not yet contain a solution for this task in any of the following software packages.

  • Excel
  • Julia

If you can contribute a solution using any of these pieces of software, see our Contributing page for how to help extend this website.