Scatter diagrams in the R programming language

Dispersion diagrams show many points drawn in Cartesian coordinates. Each point represents the values ​​of two variables. Shows one variable on the x-axis and another variable on the y-axis.

A simple dot chart is created in the R programming language using the plot () function.


Below you can see the simple syntax for creating a payload diagram in R.

plot (x, y, main, xlab, ylab, xlim, ylim, axes)

The following is an explanation of the parameters uses:

  • x is a data set whose values ​​are the horizontal coordinates of the points.
  • y is a data set whose values ​​are the vertical coordinates of the points.
  • main is the title of the chart.
  • xlab is the title of the horizontal axis (s).
  • ylab is the title of the vertical axis (s).
  • xlim is the limit of the values ​​of x uses to draw.
  • ylim is the limit of the y values ​​used to draw.
  • axes indicate whether both x and y axes should be plotted.


We use the “mtcars” dataset available in the R environment to create a simple scatter plot. We used “wt” and “mpg” columns in mtcars.

input <- mtcars [, c (‘wt’, ‘mpg’)]

print (head (input))

When we run the above code; The following result is obtained:

wt mpg

Mazda RX4 2.620 21.0

Mazda RX4 Wag 2.875 21.0

Datsun 710 2.320 22.8

Hornet 4 Drive 3.215 21.4

Hornet Sportabout 3.440 18.7

Valiant 3.460 18.1

Create scatter plots

Script below; Creates a scatter plot that shows the relationship between weight (wt) and miles per gallon of mpg:

# Get the input values.

input <- mtcars [, c (‘wt’, ‘mpg’)]

# Give the chart file a name.

png (file = “scatterplot.png”)

# Plot the chart for cars with weight between 2.5 to 5 and mileage between 15 and 30.

plot (x = input $ wt, y = input $ mpg,

xlab = “Weight”,

ylab = “Milage”,

xlim = c (2.5,5),

ylim = c (15,30),

main = “Weight vs Milage”


# Save the file. ()

When we run the above code; We achieve the following results:

Dispersion matrices

When we have more than two variables and want to find the relationship between one variable and another; We use the scatter matrix. We use the pairs () function to create scatter matrices.


The basic syntax for creating scattering matrices in R is as follows:

pairs (formula, data)

Parameters used in the above code; as follows:

  • The formula represents the set of variables used in the pairs.
  • data represents the data set from which the variable is derived.


Each variable is pairing with the remaining variable. A scatter plot is plotted for each pair:

# Give the chart file a name.

png (file = “scatterplot_matrices.png”)

# Plot the matrices between 4 variables giving 12 plots.

# One variable with 3 others and total 4 variables.

pairs (~ wt + mpg + disp + cyl, data = mtcars,

main = “Scatterplot Matrix”)

# Save the file. ()

When we run the above code; We get the following result:

Dispersion matrix:

C: \ Users \ mohammad \ Downloads \ scatterplot_matrices.png

Leave a Reply

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