How to make Boxplots in R

R Boxplots

Boxplot is a measure of how well the data is distributed in a data set. It is used to give a summary of one or several numeric variables. The line that divides the box into two parts represents the median of the data. The end of the box shows the lower and upper quartiles. The extreme lines define the highest and lowest value excluding outliers. Note that, it hides the number of values existing behind the variable. Boxplots can be created for individual variables or group of variables.

We can create Boxplot in R through the boxplot() function.

Syntax:

Here,

x is a vector

data is the data frame

notch is a logical value. To draw a notch, it must be TRUE.

varwidth is a logical value. It must be true to make box plot widths proportional to the square root of the sample sizes.

names are the labels of the group which will be printed under each boxplot.

main is the title of the graph.

Simple boxplot

Let’s create a vector and assign this vector to the boxplot() function:

Example 1:

Output:

R Box Plots Example 1

Example 2:

We will use the data set “mtcars” which is already available in the R environment to create a basic boxplot.

Let’s see the columns “mpg” (miles per gallon) and “cyl” (number of cylinders) in mtcars.

Output:

Creating the Boxplot

Let’s see an example to create a box plot graph for the relation between mpg and cyl.

Example:

Output:

R Box Plots 1

Boxplot with Notch

We can draw boxplot with the notch to find out how the medians of different data groups match with each other.

Let’s see an example to create a boxplot with the notch for each of the data group.

Example:

Output:

 R Box Plots 2

Horizontal box plot with the notch

We can plot the horizontal box plot with the notch. It means we can add a notch to the box.

Example:

Output:

R Box plots 3