plot() in R is a versatile function that supports various kinds of charts including scatter plots, line and bar graphs, his­to­grams, box plots and more.

How does plot() in R work?

The plot() function in R is used to make charts. It places data points on a co­ordin­ate plane and connects them with each other or marks them in different ways. That allows you to il­lus­trate patterns and re­la­tion­ships in data and better identify trends or de­vi­ations.

plot() can make scatter plots, line and bar graphs, his­to­grams and many other types of graphs. Thanks to its ver­sat­il­ity, it’s used for data visu­al­isa­tion in a variety of contexts, ranging from data analysis to present­ing results. Its user-friend­li­ness and flex­ib­il­ity make it an important tool for anyone analysing data, working with stat­ist­ics or looking to il­lus­trate complex data sets.

What is the syntax of plot() in R?

The structure of the plot() function in R contains arguments for x- and y-axis data and optional arguments for cus­tom­ising the ap­pear­ance of the graphic, including colours, axis labels and chart types.

The basic syntax looks as follows:

plot(x, y, ...)
R

In the following example, we plot the values from the vectors x and y on a scatter plot. The plot() function uses default values for the graph type, colours and axis labels since these arguments have been left unfilled.

x <- c(1, 2, 3, 4, 5)
y <- c(2, 4, 1, 7, 3)
plot(x, y)
R

The resulting graph looks like this:

Image: Scatter plot made with plot() in R
Scatter plot made with plot() in R

How to customise graphs with R plot()

You can customise the graph type and how it looks with further arguments in the plot() function in R.

How to create a sequence of points

You can easily create a sequence of points along the x- and y-axis using the : operator.

plot(1:20)
R

The graph will look as follows:

Image: Sequence of points made with plot() in R
Sequence of points made with plot() in R

As we can see, plot(1:20) creates a simple scatter plot where both the x-axis and the y-axis are auto­mat­ic­ally filled with the values 1 to 20.

How to draw a line

If you’d like to have a line graph rather than a scatter plot, simply enter type=l as an argument. This defines the graph type as ‘Line’.

plot(1:20, type="l")
R

A line will then be drawn between the points.

Image: Line graph made with plot() in R
Line graph made with plot() in R

How to specify labels

You can add labels to the chart with the para­met­ers main, xlab and ylab.

plot(1:20, type="l", main="Line Chart", xlab="The x-axis", ylab="The y axis")
R

The result looks as follows:

Image: Labels on a graph made with plot() in R
R graph with labels

How to change the graph’s ap­pear­ance

Let’s now look at a more complex example, where we change the colour, size and shape of the points on the graph.

plot(1:20, type = "p", col = "green", pch = 8, cex = 1.5, main = "Scatterplot", xlab = "The x-axis", ylab = "The y-axis")
R

The resulting graph looks as follows:

Image: Customising the appearance of a graph made with plot()
Cus­tom­ising the ap­pear­ance of a graph made with plot()
  • type = "p": Sets the plot type as ‘points’
  • col = "green": Sets the colour as green
  • pch = 8: Sets the symbol for the points
  • cex = 1.5 : Sets the size of the points as 1.5 times larger than standard
  • `main = “Scat­ter­plot”: Sets ‘Scat­ter­plot’ as the title of the graph
  • xlab = "The x-axis" and ylab = "The y-axis": Adds labels to the x- and y-axis

You can modify these para­met­ers as much as you want to get a graphic that fits your needs.

Tip

You can also learn how to work with strings in our Digital Guide. Take a look at the tutorials for R substring() and R gsub() and sub().

Web hosting
The hosting your website deserves at an un­beat­able price
  • Loading 3x faster for happier customers
  • Rock-solid 99.99% uptime and advanced pro­tec­tion
  • Only at IONOS: up to 500 GB included
Go to Main Menu