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:
boxplot(x, data, notch, varwidth, names, main)
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:
# Give the chart file a name.
png(file = "boxplot_simple.png")
A <- c(4, 7, 5, 2, 5, 2, 9, 1, 9, 8, 4)
boxplot(A)
# Save the file.
dev.off()
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.
input <- mtcars[,c('mpg','cyl')]
print(head(input))

Output:
                                     mpg                   cyl
Mazda RX4                            21.0                  6
Mazda RX4 Wag                        21.0                  6
Datsun 710                           22.8                  4
Hornet 4 Drive                       21.4                  6
Hornet Sportabout                    18.7                  8
Valiant                              18.1                  6

Creating the Boxplot

Let's see an example to create a box plot graph for the relation between mpg and cyl. Example:
# Give the chart file a name.
png(file = "boxplot.png")
# Plot the chart.
boxplot(mpg ~ cyl, data = mtcars, xlab = "Number of Cylinders",
ylab = "Miles Per Gallon", main = "Mileage Data")
# Save the file.
dev.off() 
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:
# Give the chart file a name.
png(file = "boxplot_with_notch.png")
# Plot the chart.
boxplot(mpg ~ cyl, data = mtcars,
xlab = "Number of Cylinders",
ylab = "Miles Per Gallon",
main = "Mileage Data",
notch = TRUE,
varwidth = TRUE,
col = c("Red","yellow","blue"),
names = c("High","Medium","Low")
)
# Save the file.
dev.off()
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:
# Give the chart file a name.
png(file = "boxplot_horizon.png")
# Plot the chart.
boxplot( mpg ~ cyl, data = mtcars,
xlab = "Number of Cylinders",
ylab = "Miles Per Gallon",
main = "Mileage Data",
notch = TRUE,
col = "green",
border = "red",
horizontal = TRUE
)
# Save the file.
dev.off()
Output: 

R Box plots 3

Related Topics

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...

3 minutes read.

Decision Tree in R Programming

The decision tree uses the branching method to show every possible output for the specific input. We can draw a decision tree by hand or create it using specialized software...

2 minutes read.

Clustering in R Programming

The clustering technique is an unsupervised machine learning in R programming. Before discussing clustering techniques, let's have a look at what unsupervised machine learning is?  Unsupervised learning is training the model with...

3 minutes read.

R Inheritance

One the main concept of object oriented programming is inheritance which allows us to define a new class of existing classes. This means that we can derive new classes from...

5 minutes read.

Introduction to R Programming

What is R Programming? R is a programming language which provides an environment for software used for statistical analysis, graphics representation and reporting. It possesses an extensive catalog of...

4 minutes read.

R Excel Files

Microsoft Excel is the spreadsheet program which stores data in the .xls and .xlsx format. R has the facility to read directly from these files using some excel specific packages....

2 minutes read.

R Pie Charts

R programming language provides many numbers of libraries to create charts and graphs. A pie-chart is a representation of data as slices of a circle with different colors representing counts...

2 minutes read.

Logistic Regression in R Programming

Logistic Regression is a classification supervised machine learning algorithm in R programming. Logistic Regression can also be termed Binomial Logistic Regression, Binary Logistic Regression, or Logit Model. Logistic Regression is...

4 minutes read.

Normal Distribution in R Programming

In statistics, the normal distribution is a type of probability function. Normal distribution tells the user about the distribution of data values in the dataset. It is a very important...

3 minutes read.

Scatter Plot in R Programming

A Scatter plot is a type of dispersion graph built to represent the various data points of variables. A Scatter plot is also known as a scatter graph or scattergram.  A scatter plot is used...

4 minutes read.

Pie Charts in R Programming

R programming has various libraries to plot various types of charts and graphs. A pie chart is a circular statistical chart or graph, which is divided into slices to represent the numerical...

4 minutes read.

How to create Histograms in R

R Histograms A Histogram is the graphical representation of the distribution of numeric data. It takes only one numeric variable as input. The variable is cut into several bars (also called...

2 minutes read.

Measures of Central Tendency in R Programming

Central Tendency or CT is one of the features of descriptive statistics. The Central tendency will let us know how the various groups of data are clustered around the central...

4 minutes read.

R Functions

In programming, a function is a group of instructions that you want to use repeatedly, because of their complexity, are better self-contained in sub-program and called when needed. Hence, we can...

4 minutes read.

R Basic Data Types

Data types are used to define the size and type of the variable. In R, there is no need to declare a variable as some data types. The variables are...

4 minutes read.

R Date and Time

R provides very large range of capabilities to deal with times and dates. Generally, dates are internally stored as integers, and depending on the operations we will need, we could...

3 minutes read.

First R Program

First  Hello World R Program As a convention, our first R program will be the “Hello World!” program. We can run our R program either at R command prompt or we...

3 minutes read.

R JSON File

JSON stands for JavaScript Object Notation. It is a lightweight format for storing and transporting data. JSON file is often used when data is sent from a server to a...

2 minutes read.

R Data Types

Data types are used to define the size and type of the variable. In R, there is no need to declare a variable as some data types. The variables are...

4 minutes read.

R Variables, Constants and Reserved Words

R Variables Variables are very similar to an open box, where you can put any value of your wish. We can change the values as well. Variables are used to store...

4 minutes read.