Scatter Plot in R Programming

The Scatter Plot in R Programming is very useful to visualize the relationship between two sets of data. The R Scatter plot displays data as a collection of points that shows the linear relation between those two data sets.

A Scatter Plot in R also called a scatter chart, scatter graph, scatter diagram, or scatter gram. For example, If we want to visualize the Age against Weight, then we can use this Scatter Plot. Let us see how to Create a Scatter Plot in R, Format its color, shape. Next, adding the linear progression to Scatter Plot in R Programming language with example.

Scatter Plot in R Syntax

The syntax to draw the scatter chart or Scatter Plot in R Programming is as shown below

plot(x, y = NULL, xlim = NULL, ylim = NULL, main = NULL)

and the complex syntax behind this R Scatter Plot is:

plot(x, y = NULL, type = "p", xlim = NULL, ylim = NULL, log = "", 
     main = NULL, sub = NULL, xlab = NULL, ylab = NULL, 
     ann = par("ann"), axes = TRUE, frame.plot = axes, 
     panel.first = NULL, panel.last = NULL, asp = NA,..)

There are many arguments supported by the Scatter Plot in R programming language, and the following are some of the arguments in real-time:

  • x, y: Please specify the data sets you want to compare. Here, you can use two separate vectors or Matrix with columns or lists.
  • type: Please specify what type of plot you want to draw.
    • To draw Points use type = “p”.
    • To draw Lines use type = “l”.
    • Use type = “h” for Histograms
    • Use type = “s” for stair steps
    • To draw over-plotted use type = “o”
  • sub: You can provide the subtitle (if any) for your scatter plot.
  • log: You have to specify a character string of three options. If X-Axis is to be logarithmic then “x”, If Y-Axis is to be logarithmic “y”, if both X-Axis and Y-Axis are to be logarithmic then specify either “xy” or “yx”
  • axes: It is a Boolean argument. If it is TRUE, the axis drew for the scatter plot in R.
  • frame.plot: It is a Boolean argument that specifies whether a box should be drawn around the plot or not.
  • panel.first: Please specify an expression evaluated after the axes drew but before the points plotted.
  • panel.last: Please specify an expression evaluated after the points plotted.
  • asp: Please specify the aspect ratio of the plot (as y/x).

How to Create a Scatter Plot in R Programming

In this example, we show how to create a scatter plot in R using the faithful data set, which is provided by the R Studio. If you require import data from external files, then refer to the R Read CSV article to understand the steps involved in CSV file import in R Programming.

# How to create a Scatter Plot in R Example
faithful

# Finding the Correlation
cor(faithful$eruptions, faithful$waiting)

# Drawing Scatter Plot
plot(faithful$eruptions, faithful$waiting)
Scatter Plot in R Programming 1

The following statement finds the correlation between the eruptions and waiting.

cor(faithful$eruptions, faithful$waiting)

faithful data set will return the output as a List. So, we are using the $ to extract the data from List.

plot(faithful$eruptions, faithful$waiting)

Assigning names to Scatter plot

In this example, we assign names to scatter plot X-Axis, Y-Axis, and individual bars using main, xlab, and ylab.

  • main: You can provide the Title for your scatter plot.
  • xlab: Please specify the label for the X-Axis
  • ylab: Please specify the label for the Y-Axis
  • las: Used to change the Y-axis values direction.
# R Scatter Plot - Changing Names Example
faithful

# Drawing Scatter Plot
plot(faithful$eruptions, faithful$waiting,
     main = "R Scatter Plot",
     xlab = "Eruptions",
     ylab = "Waiting",
     las = 1)
Scatter Plot in R Programming 2

Change Colors of Scatter plot

In this R scatter plot example, we change the scatter plot color using col argument, and size of the character that represents the point using cex argument.

  • col: Please specify the color you want to use for your Scatter plot.
  • cex: Please specify the size of the point(s).
# R Scatter Plot - Changing Color, Dot Size Example
faithful

# Drawing Scatter Plot
plot(faithful$eruptions, faithful$waiting, 
     col = "chocolate", 
     cex = 1.2, 
     main = "R Scatter Plot", 
     xlab = "Eruptions", 
     ylab = "Waiting", 
     las = 1)
Scatter Plot in R Programming 3

Change Shapes and Axis limits of Scatter Plot

In this scatter plot example, we change the shape using the pch argument.

  • xlim: This argument can help you to specify the limits for the X-Axis
  • ylim: This argument may help you to specify the Y-Axis limits
# R Scatter Plot - Changing X, Y Limitations, Dot Sape Example
faithful

# Drawing Scatter Plot
plot(faithful$eruptions, faithful$waiting, 
     col = "chocolate", 
     pch = 8, 
     main = "R Scatter Plot", 
     xlab = "Eruptions", 
     ylab = "Waiting", 
     las = 1,
     xlim = c(1.5, 5.5), 
     ylim = c(40, 100))
Scatter Plot in R Programming 4

Adding Linear Progression to Scatter Plot in R

In this example, we find the linear progression of two data sets and add them to the scatter plot using the abline function.

# R Scatter Plot - Adding Linear Progression Example
faithful

# Drawing Scatter Plot
plot(faithful$eruptions, faithful$waiting, col = "chocolate", pch = 8, 
     main = "R Scatter Plot", xlab = "Eruptions", ylab = "Waiting", las = 1)

#Linear Progression
abline(lm(faithful$waiting~faithful$eruptions), col = "red", lwd = 3)
Scatter Plot in R Programming 5

The following statement creates a table with records of sales amount and color. Here, column values are unique colors, and row values are unique sales amount.

count <- table(employee$SalesAmount, employee$Color)

Next, we are adding a line, and the line represents the linear progression.

abline(lm(faithful$waiting~faithful$eruptions), col = "red", lwd = 3)

TIP: lwd argument change the width of the line