How to Create Interaction Plot in R?
Last Updated :
26 Jan, 2022
In this article, we will discuss how to create an interaction plot in the R Programming Language.
The interaction plot shows the relationship between a continuous variable and a categorical variable in relation to another categorical variable. It lets us know whether two categorical variables have any interaction in response to a common continuous variable. If there are two parallel lines in the interaction plot, it means those two categorical variables have no interaction. Otherwise, if both lines intersect at a point that means there is an interaction between those two categorical variables.
Create a basic Interaction Plot:
To create a basic interaction plot in the R language, we use interaction.plot() function. The interaction.plot() function helps us visualize the mean/median of the response for two-way combinations of factors. This helps us in illustrating the possible interaction. The interaction.plot() function takes x.factor, trace.factor, response, and fun as arguments and returns an interaction plot layer.
Syntax:
interaction.plot( x.factor, trace.factor, response, fun )
Parameters:
- x.factor: determines the variable whose levels will form the x-axis.
- trace.factor: determines another factor whose levels will form the traces.
- response: determines a numeric variable giving the response.
- fun: determines the statistical summary element according to which trace will be made.
Example 1: Basic interaction plot
Here, is a basic interaction plot. The CSV file used in the example can be downloaded here.
R
# import sample data to data frame
sample_data <- read.csv("Sample_interaction.CSV")
# Basic Interaction Plot
interaction.plot(x.factor = sample_data$Effort,
trace.factor = sample_data$gender,
response = sample_data$Result, fun = median)
Output:

Example 2: Label Customization
To customize the x-axis and y-axis labels in the interaction plot, we use the xlab and ylab arguments of the interaction.plot() function in the R Language. To change the label of the variable in the legend of the plot, we use the trace.label argument of the interaction.plot() function in the R Language.
Syntax: interaction.plot( x.factor, trace.factor, response, fun, xlab, ylab, trace.label )
Parameters:
- xlab: determines the label for the x-axis variable.
- ylab: determines the label for the y-axis variable.
- trace.label: determines the label for the trace factor variable in legend.
Here, is a basic interaction plot with custom labels.
R
# import sample data to data frame
sample_data <- read.csv("Sample_interaction.CSV")
# Basic Interaction Plot with custom labels
interaction.plot(x.factor = sample_data$Effort,
trace.factor = sample_data$gender,
response = sample_data$Result,
fun = median, xlab="Effort",
ylab="Result", trace.label="Gender")
Output:

Example 3: Color and Shape Customization
To customize the color of the lines, we use the col parameter of the interaction.plot() function which takes a color vector as an argument. To customize the width and shape of the line, we use the lwd and lty parameters of the interaction.plot() function.
Syntax: interaction.plot( x.factor, trace.factor, response, fun, col, lwd, lty )
Parameters:
- col: determines the colors of the lines in the plot.
- lty: determines the type of line for example dashed, wedged,etc.
- lwd: determines the width of the plotline.
Here, is a basic interaction plot with custom labels, color, and shape.
R
# import sample data to data frame
sample_data <- read.csv("Sample_interaction.CSV")
# Basic Interaction Plot with custom labels
interaction.plot(x.factor = sample_data$Effort,
trace.factor = sample_data$gender,
response = sample_data$Result,
fun = median, xlab="Effort",
ylab="Result", trace.label="Gender",
col=c("green","red"),
lty=4, lwd=2.5 )
Output:
Similar Reads
How to Create and Interpret Pairs Plots in R?
In this article, we will discuss how to create and interpret Pair Plots in the R Language. The Pair Plot helps us to visualize the distribution of single variables as well as relationships between two variables. They are a great method to identify trends between variables for follow-up analysis. Pai
4 min read
How to Create a Log-Log Plot in R?
In this article, we will discuss how to create a Log-Log plot in the R Programming Language. A log-log plot is a plot that uses logarithmic scales on both the axes i.e., the x-axis and the y-axis.We can create a Log-Log plot in the R language by following methods. Log-Log Plot in Base R: To create a
2 min read
How to create Kernel Density Plot in R?
In this article, we will discuss how to create kernel density plots in R programming language. For this, the user simply needs to call the density() function which is an in-build function in R language. Then the user has to pass the given data as the parameter to this function in order to create a d
5 min read
Create interactive ggplot2 graphs with Plotly in R
"A Picture is worth a thousand words," and that picture would be even more expressive if the user could interact with it. Hence the concept of "interactive graphs or charts. Interactive charts allow both the presenter and the audience more freedom since they allow users to zoom in and out, hover and
6 min read
How to Create a Population Pyramid in R?
In this article, we will discuss how to create a population pyramid in the R Programming Language. A population pyramid is also known as an age-sex pyramid. It helps us to visualize the distribution of a population by age group and sex. It generally takes the shape of a pyramid. In the population py
4 min read
How to Create a Forest Plot in R?
In this article, we will discuss how to create a Forest Plot in the R programming language. A forest plot is also known as a blobbogram. It helps us to visualize estimated results from a certain number of studies together along with the overall results in a single plot. It is extensively used in med
4 min read
Interactive Charts using Plotly in R
R Programming Language is a powerful tool for data analysis and visualization. Interactive plots with R can be particularly useful for exploring and presenting data, but creating them can be challenging. The Shiny package provides a framework for creating web-based applications with R, including int
5 min read
How to Overlay Plots in R?
In this article, we will discuss how to overlay plots in the R Programming Language. Overlaying is a technique that is used to draw multiple plots on a single frame. To draw multiple plots in the R Language, we draw a basic plot and add an overlay line plot or scatter plot by using the lines() and t
3 min read
Creating Interactive Plots using Shiny
Data visualization is an essential part of data analysis, allowing us to explore trends, patterns, and relationships within our data. While static plots are informative, interactive plots take visualization to the next level, enabling users to interact with the data dynamically. Shiny AppShiny is an
7 min read
How to plot excel data in R?
Plotting graph in R using an excel file, we need an excel file with two-column in it, the values in the first column will be considered as the points at the x-axis and the values in the second column will be considered as the points at the y-axis. In this article, we will be discussing the approach
2 min read