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

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.

R Line Graphs

Line graph or line chart is a graph that connects a series of points by drawing segments between them. A line graph represents the relationship between 2 variables. These points...

2 minutes read.

Analysis of Covariance in R Programming

Analysis of Covariance can also be named ANCOVA. We know that we use the concept of regression analysis for creating models which can explain the effect of the variation in...

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

Linear Regression in R Programming

Linear Regression is an unsupervised machine learning algorithm in R programming. Linear Regression is a type of regression analysis that shows the relationship between two or more variables. And one...

3 minutes read.

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.

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.

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 Tutorial

Introduction to R Programming R is nothing but a programming language, also a free software environment. This software used for predictive analysis, statistical analysis, graphical representation, data modeling, and reporting. R...

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

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.

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.

Various types of Charts in R Programming

As we know, R Language is mostly used for analyzing the data and statistics purposes to represent our data graphically & pictorially in R studio. Charts and graphs are plotted in...

4 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 If Else Statement

If statement It is the most simple decision-making statement. It checks the condition if the condition is true then only it will execute the block of statements written in the ‘if...

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.

Binomial Distribution in R Programming

In this article, we will talk about the Binomial distribution in R programming. The binomial distribution is a type of probability distribution. As it is a discrete distribution, it will...

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

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 Operators

A symbol that tells the compiler to perform specific mathematical or logical operations is called operator. R language supports mainly 5 different types of operators, which are listed below: Arithmetic...

3 minutes read.