R plot() Function

The plot() function in R is used to create a wide range of graphs, including scatter plots, line plots, and more, depending on the type and structure of the data provided. It has many options and arguments to control many things, such as the plot type, labels, titles, colors, etc.

Syntax

plot(x, y, type, main, xlab, ylab, pch, col, las, bty, bg, cex, )

Parameters

The is the coordinates of points in the plot.

The y is the coordinates of points in the plot.

main: It is an overall title for the plot.

xlab: It is a label for the x-axis.

ylab: It is the label for the y-axis.

pch: It is the shape of points.

col: It is the foreground color of symbols as well as lines.

las: It is the axes label style.

bty: It is the type of box around the plot area.

bg: It is the background color of symbols (only 21 through 25).

cex: It is an amount of scaling plotting text and symbols.

… They are the arguments to be passed to methods.

  1. p” for points plot,
  2. l” for a line plot,
  3. b” for both plots,
  4. c” for the part of the line alone of “b”,
  5. o” for both “overplotted”,
  6. h” for ‘histogram’ like (or “high-density“) vertical lines.
  7. s” for stair steps,
  8. S” for other steps. See ‘Details’ below,
  9. n” for no plotting.

Example 1: Simple plot() function implementation

Let’s use the equation y = x^3. This means we will define two vectors, x and y, and y is the cube of x.

x <- c(1, 2, 3, 4, 5)
y <- c(1, 8, 27, 64, 125)

Let’s plot the y = x^3 values on the line plot. To create a line plot, pass the parameter type = “l” inside the plot function.

x <- c(1, 2, 3, 4, 5)
y <- c(1, 8, 27, 64, 125)

plot(x, y, type = "l")

Output

Simple plot() function implementation in R

And we get the line chart of the y = x^3 function.

If we don’t pass the type = “l” in the argument, it will return the points plot.

By default, the plot() function returns a point plot.

point plot in R

Example 2: Plotting the cos() function

x <- seq(-pi, pi, 0.1)

plot(x, cos(x))

Output

Plot the cos() function

Example 3: Adding titles and labeling axes

To add a title to our plot, use the main parameter and pass the name of your choice.

To label the x and y-axis, use the xlab and ylab arguments.

x <- seq(-pi, pi, 0.1)

plot(x, cos(x), main = "Cos Function", ylab = "cos(x)")

Output

Adding Titles and Labeling Axes

Example 4: Changing the symbols and colors of a plot

We can see above that the plot is of circular points and is black in color. This is the default color.

Let’s change the symbol using the pch and col parameters for choosing the color.

x <- seq(-pi, pi, 0.1)

plot(x, cos(x), pch = c(4, 5, 6), col = c("red", "blue", "violet", "green"))

Output

Changing the symbols and colors

Example 5: Plot multiple graphs into a single image

To combine multiple graphs into a single image, use the par() function.

Let’s combine two graphs. 1st is a line chart, and 2nd is a point chart with different symbols and colors.

par(mfrow = c(1, 2))
x <- seq(-pi, pi, 0.1)
plot(x, cos(x), type="l")
plot(x, cos(x), pch = c(4, 5, 6), col = c("red", "blue", "violet", "green"))

Output

Plot multiple graphs into a single image

You can also add more graphs using the par() function. For example, let’s add six graphs in one image.

par(mfrow = c(2, 3))

x <- seq(-pi, pi, 0.1)
plot(x, cos(x), type = "l")
plot(x, cos(x), pch = c(4, 5, 6), col = c("red", "blue", "violet", "green"))

plot(x, x ^ 3, col = "red", type = "l")

m <- 0.8
c <- 2
plot(x, m * x + c, col = "green", type = "o", lwd = 2, lty = 1)
plot(x, log(x), col = "violet", type = "s")
plot(x, exp(x), col = "red", type = "b")

Output

plot six graphs in one image

Example 6: Overlaying Plots Using legend() function

Sometimes, we need to overlay the plots to compare the results. To overlay the plot, use the lines() and points() methods to add lines and points to the existing plot.

x <- seq(-pi, pi, 0.1)

plot(x, cos(x), type = "l",
 main = "Overlaying Charts",
 ylab = "",
 col = "red")

lines(x, sin(x), col = "blue")

legend("topleft", c("sin(x)", "cos(x)"), fill = c("blue", "red"))

Output

overlaying graphs in R

Example 7: Adding lines to Plot

To add the straight line to the existing plot, use the abline() function. It takes four parameters, a, b, h, and v. The variables, a and b represent the slope and intercept. The h represents the y points for horizontal lines, and the v represents the x points for vertical lines.

x <- seq(1, 10, 2)
y1 <- x ^ 2
y2 <- x ^ 3

plot(x, y1, type = "l", col = "red")
lines(x, y2, col = "green")
legend("bottomright", inset = 0.05, c("Squares", "Cubes"), 
       lty = 1, col = c("red", "green"), 
       title = "Graph type")

abline(a = 4, b = 5, col = "blue")
abline(h = c(4, 6, 8), col = "red", lty = 2)
abline(v = c(4, 6, 8), col = "green", lty = 2)

Output

Adding Lines to a Plot

That’s all!

Leave a Comment