Scatter plots in R Language
Last Updated :
10 May, 2023
A scatter plot is a set of dotted points representing individual data pieces on the horizontal and vertical axis. In a graph in which the values of two variables are plotted along the X-axis and Y-axis, the pattern of the resulting points reveals a correlation between them.
R – Scatter plots
We can create a scatter plot in R Programming Language using the plot() function.
Syntax: plot(x, y, main, xlab, ylab, xlim, ylim, axes)
Parameters:
- x: This parameter sets the horizontal coordinates.
- y: This parameter sets the vertical coordinates.
- xlab: This parameter is the label for horizontal axis.
- ylab: This parameter is the label for vertical axis.
- main: This parameter main is the title of the chart.
- xlim: This parameter is used for plotting values of x.
- ylim: This parameter is used for plotting values of y.
- axes: This parameter indicates whether both axes should be drawn on the plot.
Simple Scatterplot Chart
In order to create Scatterplot Chart:
- We use the data set “mtcars”.
- Use the columns “wt” and “mpg” in mtcars.
Example:
R
input <- mtcars[, c('wt', 'mpg')]
print(head(input))
|
Output:

Creating a Scatterplot Graph
In order to create an R Scatterplot graph:
- We are using the required parameters to plot the graph.
- In this ‘xlab’ describes the X-axis and ‘ylab’ describes the Y-axis.
Example:
R
input <- mtcars[, c('wt', 'mpg')]
plot(x = input$wt, y = input$mpg,
xlab = "Weight",
ylab = "Milage",
xlim = c(1.5, 4),
ylim = c(10, 25),
main = "Weight vs Milage"
)
|
Output:

Scatter plots in R Language
Scatterplot Matrices
When we have two or more variables and we want to correlate between one variable and others so we use a R scatterplot matrix.
pairs() function is used to createR matrices of scatterplots.
Syntax: pairs(formula, data)
Parameters:
- formula: This parameter represents the series of variables used in pairs.
- data: This parameter represents the data set from which the variables will be taken.
Example:
R
pairs(~wt + mpg + disp + cyl, data = mtcars,
main = "Scatterplot Matrix")
|
Output:

Scatter plots in R Language
Scatterplot with fitted values
In order to create R Scatterplot Chart:
- We are using the ggplot2 package provides ggplot() and geom_point() function for creating a scatterplot.
- Also we are using the columns “wt” and “mpg” in mtcars.
Example:
R
library(ggplot2)
ggplot(mtcars, aes(x = log(mpg), y = log(drat))) +
geom_point(aes(color = factor(gear))) +
stat_smooth(method = "lm",
col = "#C42126", se = FALSE, size = 1
)
|
Output:

Scatter plots in R Language
Adding title with dynamic name
To create R Scatterplot Chart, Add a sub-title:
- We use the additional function, In ggplot we add the data set “mtcars” with this adding ‘aes’, ‘geom_point’.
- Use the Title, Caption, Subtitle.
Example:
R
library(ggplot2)
new_graph<-ggplot(mtcars, aes(x = log(mpg),
y = log(drat))) +
geom_point(aes(color = factor(gear))) +
stat_smooth(method = "lm",
col = "#C42126",
se = FALSE, size = 1)
new_graph + labs(
title = "Relation between Mile per hours and drat",
subtitle = "Relationship break down by gear class",
caption = "Authors own computation"
)
|
Output:

Scatter plots in R Language
3D Scatterplots
Here we will use R scatterplot3D package to create 3D scatterplots, this package can plot R scatterplots in 3D using scatterplot3d() methods.
R
library(plotly)
attach(mtcars)
plot_ly(data=mtcars,x=~mpg,y=~hp,z=~cyl,color=~gear)
|
Output:

Scatter plots in R Language
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...