11.1 C
London
Sunday, July 7, 2024
HomeStatistics TutorialRHow to Plot Multiple Plots on Same Graph in R (3 Examples)

How to Plot Multiple Plots on Same Graph in R (3 Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following methods to plot multiple plots on the same graph in R:

Method 1: Plot Multiple Lines on Same Graph

#plot first line
plot(x, y1, type='l')

#add second line to plot
lines(x, y2)

Method 2: Create Multiple Plots Side-by-Side

#define plotting area as one row and two columns
par(mfrow = c(1, 2))

#create first plot
plot(x, y1, type='l')

#create second plot
plot(x, y2, type='l')

Method 3: Create Multiple Plots Stacked Vertically

#define plotting area as two rows and one column
par(mfrow = c(2, 1))
  
#create first plot
plot(x, y1, type='l')

#create second plot
plot(x, y2, type='l')

The following examples show how to use each method in practice.

Example 1: Plot Multiple Lines on Same Graph

The following code shows how to plot two lines on the same graph in R:

#define data to plot
x #plot first line
plot(x, y1, type='l', col='red', xlab='x', ylab='y')

#add second line to plot
lines(x, y2, col='blue')

R plot multiple plots in same graph

Example 2: Create Multiple Plots Side-by-Side

The following code shows how to use the par() argument to plot multiple plots side-by-side:

#define data to plot
x 

#define plotting area as one row and two columns
par(mfrow = c(1, 2))

#create first line plot
plot(x, y1, type='l', col='red')

#create second line plot
plot(x, y2, type='l', col='blue', ylim=c(min(y1), max(y1)))

Note that we used the ylim() argument in the second plot to ensure that the two plots had the same y-axis limits.

Example 3: Create Multiple Plots Stacked Vertically

The following code shows how to use the par() argument to plot multiple plots stacked vertically:

#define data to plot
x 

#define plotting area as two rows and one column
par(mfrow = c(2, 1), mar = c(2, 4, 4, 2))
#create first line plot
plot(x, y1, type='l', col='red')

#create second line plot
plot(x, y2, type='l', col='blue', ylim=c(min(y1), max(y1)))

Note that we used the mar argument to specify the (bottom, left, top, right) margins for the plotting area.

Note: The default is mar = c(5.1, 4.1, 4.1, 2.1)

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Plot Multiple Columns in R
How to Draw a Legend Outside of a Plot in R
How to Create a Log-Log Plot in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories