R Plots Side By Side

Posted onby admin

In this article, you will learn to create whisker and box plot in R programming. You will also learn to draw multiple box plots in a single plot.


In R, boxplot (and whisker plot) is created using the boxplot() function.

The boxplot() function takes in any number of numeric vectors, drawing a boxplot for each vector.

R Plots Side By Side

Otherwise, all your subsequent plots will appear side by side (until you close the active graphics device, or window, and start plotting in a new graphics device). You can use a neat little trick to do this: When you make a call to par , R sets your new options, but the return value from par contains your old options. R makes it easy to combine multiple plots into one overall graph, using either the par or layout function. With the par function, you can include the option mfrow=c (nrows, ncols) to create a matrix of nrows x ncols plots that are filled in by row. Mfcol=c (nrows, ncols) fills in the matrix by columns. Doing a side by side boxplot in R involves using the boxplot function which has the form of boxplot (data sets) and produces a side by side boxplot graph of the data sets it is being applied to. You can enter one or more data sets. This function also has several optional parameters, including r boxplot options like.

How to plot side-by-side Plots with ggplot2 in R. How to plot side-by-side Plots with ggplot2 in R. Like we are creating two plots of the dataset 'iris'.

You can also pass in a list (or data frame) with numeric vectors as its components. Let us use the built-in dataset airquality which has “Daily air quality measurements in New York, May to September 1973.”-R documentation.

Let us make a boxplot for the ozone readings.

We can see that data above the median is more dispersed. We can also notice two outliers at the higher extreme.

R 3 Plots Side By Side

We can pass in additional parameters to control the way our plot looks. You can read about them in the help section ?boxplot.

Some of the frequently used ones are, main-to give the title, xlab and ylab-to provide labels for the axes, col to define color etc.

Additionally, with the argument horizontal = TRUE we can plot it horizontally and with notch = TRUE we can add a notch to the box.

Return Value of boxplot()

The boxplot() function returns a list with 6 components shown as follows.

As we can see above, a list is returned which has stats-having the position of the upper/lower extremes of the whiskers and box along with the median,

  • n-the number of observation the boxplot is drawn with (notice that NA‘s are not taken into account)
  • conf-upper/lower extremes of the notch, out-value of the outliers
  • group-a vector of the same length as out whose elements indicate to which group the outlier belongs and
  • names-a vector of names for the groups.

Multiple Boxplots

We can draw multiple boxplots in a single plot, by passing in a list, data frame or multiple vectors.

Let us consider the Ozone and Temp field of airquality dataset. Let us also generate normal distribution with the same mean and standard deviation and plot them side by side for comparison.

Now we us make 4 boxplots with this data. We use the arguments at and names to denote the place and label.

Boxplot form Formula

The function boxplot() can also take in formulas of the form y~x where, y is a numeric vector which is grouped according to the value of x.

For example, in our dataset airquality, the Temp can be our numeric vector. Month can be our grouping variable, so that we get the boxplot for each month separately. In our dataset, month is in the form of number (1=January, 2-Febuary and so on).

It is clear from the above figure that the month number 7 (July) is relatively hotter than the rest.


Side By Side Boxplots R

  • PREVIOUS
    R Pie Chart
  • NEXT
    R Strip Chart