What is the plot() function in R?
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)
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))
Example 3
For a number of points, one needs to provide the same number of points for both axes.
# creating equal axisx <- c(1, 2, 3, 4, 5)y <- c(3, 6, 9, 12, 15)# implementing the plot() functionplot(x, y)
Explanation
- Lines 2 and 3: We create the values of the points in the
xandyaxis, respectively. We use their corresponding names (xandy) as their variable names. - Line 6: We plot the various points in the
xandyaxis using theplot()function.