15.1 C
London
Friday, July 5, 2024
HomeStatistics TutorialRHow to Create a Scatterplot in R with Multiple Variables

How to Create a Scatterplot in R with Multiple Variables

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 basic syntax to create a scatterplot with multiple variables in R:

#create scatterplot of x1 vs. y1
plot(x1, y1, col='red')

#add scatterplot of x2 vs. y2
points(x2, y2, col='blue')

#add legend
legend(1, 25, legend=c('Data 1', 'Data 2'), pch=c(19, 19), col=c('red', 'blue'))

The following examples show how to use this syntax in practice.

Example 1: Create Scatterplot with Two Variables

The following code shows how to create a scatterplot with two different variables:

#define datasets
x1 = c(1, 3, 6, 11, 19, 20)
y1 = c(7, 10, 11, 12, 18, 25)

x2 = c(1, 3, 8, 13, 17, 19)
y2 = c(9, 15, 18, 21, 22, 22)

#create scatterplot of x1 vs. y1
plot(x1, y1, col='red', pch=19)

#add scatterplot of x2 vs. y2
points(x2, y2, col='blue', pch=19)

#add legend
legend(1, 25, legend=c('Data 1', 'Data 2'), pch=c(19, 19), col=c('red', 'blue'))

Example 2: Customize the Scatterplot

The following code shows how to customize the axes labels, title, and size of the points in the plot:

#define datasets
x1 = c(1, 3, 6, 11, 19, 20)
y1 = c(7, 10, 11, 12, 18, 25)

x2 = c(1, 3, 8, 13, 17, 19)
y2 = c(9, 15, 18, 21, 22, 22)

#create scatterplot of x1 vs. y1
plot(x1, y1, col='red', pch=19, cex=1.3,
     xlab='X', ylab='Y', main='Scatterplot of Two Variables')

#overlay scatterplot of x2 vs. y2
points(x2, y2, col='blue', pch=19, cex=1.3)

#add legend
legend(1, 25, legend=c('Data 1', 'Data 2'), pch=c(19, 19), col=c('red', 'blue'))

Scatterplot of multiple variables in R

Note that the pch argument specifies the shape of the points in the plot. A pch value of 19 specifies a filled-in circle.

You can find a complete list of pch values and their corresponding shapes here.

Additional Resources

How to Label Points on a Scatterplot in R
How to Create a Scatterplot with a Regression Line in R
How to Use the Jitter Function for Scatterplots 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