An interaction plot is a type of plot that displays the mean values of multiple groups for two different variables.
This type of plot is used often in designed experiments, ANOVA, and regression analysis.
This tutorial provides a step-by-step example of how to create an interaction plot in R using the ggplot2 data visualization library.
Step 1: Create the Data
For this example, we’ll create a fake dataset that contains the following three variables for eight different students:
- Studying Technique Used (‘A’ or ‘B’)
- Hours Spent Studying
- Final Exam Score
The following code shows how to create this dataset:
#create data df <- data.frame(type=c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'), hours=c(1, 1, 2, 2, 1, 1, 2, 2), score=c(80, 82, 89, 93, 76, 79, 95, 99)) #view data df type hours score 1 A 1 80 2 A 1 82 3 A 2 89 4 A 2 93 5 B 1 76 6 B 1 79 7 B 2 95 8 B 2 99
Step 2: Create the Interaction Plot
Next, we’ll create an interaction plot to visualize the average exam score based on total hours spent studying and studying technique used:
library(dplyr) library(ggplot2) #find mean exam score based on hours studied and study type summary_data <- df %>% group_by(hours, type) %>% summarise(score_groups = mean(score)) #create interaction plot summary_data %>% ggplot() + aes(x = hours, y = score_groups, color = type) + geom_line(aes(group = type)) + geom_point()
Here is how to interpret the plot:
- The x-axis displays the total hours spent studying
- The y-axis displays the mean exam score
- The two lines display the mean exam score based on studying technique used
We can see that the mean exam score is higher for the students who studied more hours, regardless of the studying technique they used.
However, since the two lines cross each other this is an indication that there is an interaction effect present.
This means that the exam score is dependent on an interaction between studying technique used and number of hours spent studying.
Step 3: Modify the Interaction Plot (Optional)
Lastly, we can modify the axis labels, title, and general aesthetics of the plot to make it look a bit better:
#create interaction plot with better appearance summary_data %>% ggplot() + aes(x = hours, y = score_groups, color = type) + geom_line(aes(group = type)) + geom_point() + labs(title='Exam Score by Hours Studied & Studying Technique', color='Technique', x='Hours Spent Studying', y='Mean Exam Score') + theme_minimal()
Note: We chose to use theme_minimal() for the plot theme, but you can find a complete list of the best ggplot2 themes here.
You can find more R tutorials on this page.