R Plot Function

In this article, you will learn to use the plot function in R programming with the help of examples.

The most used plotting function in R programming is the plot() function. It is a generic function, meaning, it has many methods which are called according to the type of object passed to plot().

In the simplest case, we can pass in a vector and get a scatter plot of magnitude vs index. But generally, we pass in two vectors, and a scatter plot of these points is plotted.

For example, the command plot(c(1,2),c(3,5)) would plot the points (1,3) and (2,5).

Here is a more concrete example where we plot a sine function ranging from -pi to pi.

x <- seq(-pi,pi,0.1)
plot(x, sin(x))
R Plot Function Example
R Plot Function Example

Here, at first, we generate a sequence of numbers from to π with a step size of 0.1. The generated sequence will be a vector containing values like -3.141593, -3.041593, -2.941593, ..., 3.041593, 3.141593.

plot(x, sin(x)) creates a plot of the sine function using plot() where x is the vector we created before.


Adding Titles and Labeling Axes

We can add a title to our plot with the parameter main. Similarly, xlab and ylab can be used to label the x-axis and y-axis respectively.

plot(x, sin(x),
main="The Sine Function",
ylab="sin(x)")
Adding titles and labeling axes of a plot in R Programming
Adding titles and labeling axes of a plot in R Programming

Here, the plot now has a title indicating the nature of the function being plotted ("The Sine Function") and a label on the y-axis to indicate the scale or interpretation of the y-values on the plot ("sin(x)").


Changing Color and Plot Type

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

We can change the plot type with the argument type. It accepts the following strings and has the given effect.

"p" - points
"l" - lines
"b" - both points and lines
"c" - empty points joined by lines
"o" - overplotted points and lines
"s" and "S" - stair steps
"h" - histogram-like vertical lines
"n" - does not produce any points or lines

Similarly, we can define the color using col.

plot(x, sin(x),
main="The Sine Function",
ylab="sin(x)",
type="l",
col="blue")
Coloring a plot in R programming
Coloring a plot in R programming

Overlaying Plots Using legend() function

Calling plot() multiple times will have the effect of plotting the current graph on the same window replacing the previous one.

However, sometimes we wish to overlay the plots in order to compare the results.

This is made possible with the functions lines() and points() to add lines and points respectively, to the existing plot.

plot(x, sin(x),
main="Overlaying Graphs",
ylab="",
type="l",
col="blue")
lines(x,cos(x), col="red")
legend("topleft",
c("sin(x)","cos(x)"),
fill=c("blue","red")
)
Overlaying plots in R Using legend() function
Overlaying plots in R Using legend() function

We have used the function legend() to appropriately display the legend. Visit legend() function to learn more.

Also visit the plot() function to learn more about the different arguments plot() function can take, and more examples.

Did you find this article helpful?