# R Boxplot

Boxplots are a measure of how well data is distributed across a data set. This divides the data set into three quartiles. This graph represents the minimum, maximum, average, first quartile, and the third quartile in the data set. Boxplot is also useful in comparing the distribution of data in a data set by drawing a boxplot for each of them.

R provides a boxplot() function to create a boxplot. There is the following syntax of boxplot() function:

boxplot(x, data, notch, varwidth, names, main)

Here,

S.No | Parameter | Description |
---|---|---|

1. | x | It is a vector or a formula. |

2. | data | It is the data frame. |

3. | notch | It is a logical value set as true to draw a notch. |

4. | varwidth | It is also a logical value set as true to draw the width of the box same as the sample size. |

5. | names | It is the group of labels that will be printed under each boxplot. |

6. | main | It is used to give a title to the graph. |

Let?s see an example to understand how we can create a boxplot in R. In the below example, we will use the “mtcars” dataset present in the R environment. We will use its two columns only, i.e., “mpg” and “cyl”. The below example will create a boxplot graph for the relation between mpg and cyl, i.e., miles per gallon and number of cylinders, respectively.

### Example

`# Giving a name to the chart file. png(file = "boxplot.png") # Plotting the chart. boxplot(mpg ~ cyl, data = mtcars, xlab = "Quantity of Cylinders", ylab = "Miles Per Gallon", main = "R Boxplot Example") # Save the file. dev.off()`

**Output:**

## Boxplot using notch

In R, we can draw a boxplot using a notch. It helps us to find out how the medians of different data groups match with each other. Let’s see an example to understand how a boxplot graph is created using notch for each of the groups.

In our below example, we will use the same dataset ?mtcars."

### Example

`# Giving a name to our chart. png(file = "boxplot_using_notch.png") # Plotting the chart. boxplot(mpg ~ cyl, data = mtcars, xlab = "Quantity of Cylinders", ylab = "Miles Per Gallon", main = "Boxplot Example", notch = TRUE, varwidth = TRUE, ccol = c("green","yellow","red"), names = c("High","Medium","Low") ) # Saving the file. dev.off()`

**Output:**

## Violin Plots

R provides an additional plotting scheme which is created with the combination of a **boxplot** and a **kernel density** plot. The violin plots are created with the help of vioplot() function present in the vioplot package.

Let’s see an example to understand the creation of the violin plot.

### Example

`# Loading the vioplot package library(vioplot) # Giving a name to our chart. png(file = "vioplot.png") #Creating data for vioplot function x1 <- mtcars$mpg[mtcars$cyl==4] x2 <- mtcars$mpg[mtcars$cyl==6] x3 <- mtcars$mpg[mtcars$cyl==8] #Creating vioplot function vioplot(x1, x2, x3, names=c("4 cyl", "6 cyl", "8 cyl"), col="green") #Setting title title("Violin plot example") # Saving the file. dev.off()`

**Output:**

## Bagplot- 2-Dimensional Boxplot Extension

The bagplot(x, y) function in the **aplpack** package provides a biennial version of the univariate boxplot. The bag contains 50% of all points. The bivariate median is approximate. The fence separates itself from the outside points, and the outlays are displayed.

Let?s see an example to understand how we can create a two-dimensional boxplot extension in R.

### Example

`# Loading aplpack package library(aplpack) # Giving a name to our chart. png(file = "bagplot.png") #Creating bagplot function attach(mtcars) bagplot(wt,mpg, xlab="Car Weight", ylab="Miles Per Gallon", main="2D Boxplot Extension") # Saving the file. dev.off()`

**Output:**