What is the plot() function in R?

Share

Overview

The plot() function in R is used to draw or create graphs and charts for visualizations. In simple terms, the plot() function is used to return a plot of a number(s) against another number(s).

Syntax

plot(x, y)

Parameters

The plot() function takes two parameter values:

  • x: Represents points on the x-axis of the plot.
  • y: Represents points on the y-axis of the plot.

Example 1

Let’s create a plot of the number 2 against 6 using the plot() function.

plot(2, 6)
Output of the code

Example 2

The plot() function can also be used to draw two points in a diagram, where one point represents a position at (x, y) and another represents another position at (x, y).

plot(c(2, 6), c(4, 12))
Output of the code

Example 3

For a number of points, one needs to provide the same number of points for both axes.

# creating equal axis
x <- c(1, 2, 3, 4, 5)
y <- c(3, 6, 9, 12, 15)
# implementing the plot() function
plot(x, y)
Implementing the plot() function for multiple points

Explanation

  • Lines 2 and 3: We create the values of the points in the x and y axis, respectively. We use their corresponding names (x and y) as their variable names.
  • Line 6: We plot the various points in the x and y axis using the plot() function.