Skip to content

Data Science Tutorials

For Data Science Learners

  • Difference between R and Python
    Difference between R and Python R
  • sorting in r
    Sorting in r: sort, order & rank R Functions R
  • Hypothesis Testing in R Programming
    Hypothesis Testing in R Programming R
  • Error in rbind(deparse.level ...) numbers of columns of arguments do not match
    Error in rbind(deparse.level …) numbers of columns of arguments do not match R
  • Cross-validation in Machine Learning
    Cross-validation in Machine Learning Statistics
  • Error in sum(List) : invalid 'type' (list) of argument
    Error in sum(List) : invalid ‘type’ (list) of argument R
  • Error: Can't rename columns that don't exist
    Can’t rename columns that don’t exist R
  • How to Find Quartiles in R
    How to Find Quartiles in R? R
How to Create an Interaction Plot in R

How to Create an Interaction Plot in R?

Posted on July 27July 27 By Admin No Comments on How to Create an Interaction Plot in R?

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.

Check your inbox or spam folder to confirm your subscription.

R Tags:interaction

Post navigation

Previous Post: How to Create Summary Tables in R
Next Post: How to Standardize Data in R?

Related Posts

  • Check whether any values of a logical vector are TRUE
    Check whether any values of a logical vector are TRUE R
  • Best Git Books R
  • How to Visualize PCA Results in R
    How to Visualize PCA Results in R R
  • Duplicate and concatenate in R R
  • What is bias variance tradeoff
    What is the bias variance tradeoff? R
  • Difference between R and Python
    Difference between R and Python R

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Maximizing Model Accuracy with Train-Test Splits in Machine Learning
  • Type II Errors in R
  • Best Prompt Engineering Books
  • Understanding Machine Learning and Data Science
  • Best Git Books
  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy

https://www.r-bloggers.com

  • YouTube
  • Twitter
  • Facebook
  • Course
  • Excel
  • Machine Learning
  • Opensesame
  • R
  • Statistics

Check your inbox or spam folder to confirm your subscription.

  • Tips for Data Scientist Interview Openings
    Tips for Data Scientist Interview Openings Course
  • Steps to Mastering Natural Language Processing
    Steps to Mastering Natural Language Processing Machine Learning
  • How to Use Bold Font in
    How to Use Bold Font in R with Examples R
  • How to copy files in R
    How to copy files in R R
  • Predictive Modeling and Data Science
    Predictive Modeling and Data Science Machine Learning
  • How to perform kruskal wallis test in r
    How to perform the Kruskal-Wallis test in R? R
  • Ad Hoc Analysis
    What is Ad Hoc Analysis? Statistics
  • sorting in r
    Sorting in r: sort, order & rank R Functions R

Privacy Policy

Copyright © 2025 Data Science Tutorials.

Powered by PressBook News WordPress theme