×


R Plot Types

In this article, you will learn different types of plotting functions in R.

There are many plotting functions available in R and they are categories in three basic types.

  • High Level Plotting Functions
  • Low Level Plotting Functions
  • Interactive Plots Functions

Here, we explain all the types with examples.

High Level Plotting Functions

High level plotting functions are used to create a new plot. It generates a complete plot of data passed as arguments to the function. These are the plot functions come under high level plot.

plot() function

The plot() function is especially to show the relationship between two variables. One of the two variables is scaled horizontally and the other is scaled vertically, and the graph is plotted on a Cartesian plane based on these variable positions.

Syntax of plot() function

plot(x, y, main, xlab, ylab, xlim, ylim, pch)

Example

We store the quality and sales values in two different vectors and pass these as an argument in the plot function.

quantity <- c(400, 300, 350, 300, 250, 200)
sales <- c(350, 400, 340, 300, 200, 250)
plot(quantity, sales, main='Sales Chart', pch=16)


boxplot() function

Boxplot is a visual representation of data that shows the minimum, first quartile, median, third quartile and maximum in the graph. The median is a line that divides the box into two parts.

Syntax of boxplot() function

boxplot(x, data, col, xlab, ylab, main, notch, varwidth)

Example

In the example below, we generate a box plot of two vectors and also set box colors.

students <- c(10, 15, 10, 17, 12)
marks <- c(9, 7, 8, 6, 10)
boxplot(students, marks, col <- c('blue', 'red'))


piechart() function

The pie chart is a circular statical graph, which represent the values in slices. In R, we can generate pie chart using pie() function.

Syntax of piechart() function

pie(x, labels, radius, main, col, clockwise) 

Example

In the example below, we generate a pie chart with two vectors and also set colors.

people <- c(24, 32, 34, 30, 40)
food <- c('Sandwich','Pizza','Donuts','Noodles','Burger')
pie(people, food, col=c('red','yellow','purple','blue','green'))



Low Level Plotting Functions

Low level plotting functions add more information to the existing plot. These are the plot functions come under low level plot.

abline()

This function adds one or more straight lines (vertical, horizontal or regression lines) to the current plot.

axis(side, ...)

It adds an axis to the current plot on the side given by the first argument and other arguments control the positioning of the axis within or beside the plot.

lines(x, y)

It adds points or connected lines to the current plot.

text(x, y, labels)

It adds text to a plot at the coordinates given by x and y.

polygon(x, y)

It draws the polygons whose vertices are given in x and y.

legend(x, y)

It adds a legend to the current plot at the specified position.

title(main, sub)

It adds a title main to the top of the current plot in a large font and a subtitle at the bottom in a smaller font. The subtitle (sub) is optional.



Interactive Plots Functions

Interactive plots functions allow users to extract or add information to a plot. These are the functions come under interactive plots.

identify()

It display the position of the graphics or the row number or name for the point when the mouse button is pressed.

locator()

It is used to add points or lines to the plot by the mouse.





Read more articles


General Knowledge



Learn Popular Language