R: How to Specify Width in geom_boxplot


A boxplot is a type of plot that allows us to visualize the five number summary of a variable, which includes:

  • The minimum
  • The first quartile
  • The median
  • The third quartile
  • The maximum

By knowing these five values, we can gain a strong understanding of the distribution of values in a particular variable.

The easiest way to create a boxplot in R is by using the geom_boxplot() function from the ggplot2 package.

Often when using the geom_boxplot() function you may wish to adjust the width of the actual boxplot.

You can use the following basic syntax to do so:

library(ggplot2)

ggplot(mtcars, aes(y=mpg)) +
  geom_boxplot(width=0.5) +
  xlim(-1,1)

This particular example creates a boxplot of the variable mpg from the mtcars dataset and specifies that the width of the boxplot should be 0.5.

We also use the xlim() argument to specify the lower and upper limits of the x-axis.

Note that we must include the xlim() argument as well or else the width argument in geom_boxplot() will not modify the width shown in the plot.

The following example shows how to adjust the width of a boxplot created using the geom_boxplot() function to from ggplot2 in practice.

Note: You may first need to install the ggplot2 package to be able to use the various plotting functions in it. You can use the following syntax to do so:

install.packages('ggplot2')

Once you have this package installed, you can use the functions from it without receiving any errors.

Example: How to Specify Width in geom_boxplot in ggplot2

For this particular example, we will use the built-in mtcars dataset in R.

We can use the head() function to view the first six rows of this dataset:

#view first six rows of mtcars dataset
head(mtcars)

                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

The dataset contains several variables that tell us different information about various cars.

Suppose that we would like to create a boxplot of the mpg variable, which represents the miles per gallon of each car in the dataset.

We can use the following syntax to do so:

library(ggplot2)

#create boxplot of mpg
ggplot(mtcars, aes(y=mpg)) +
  geom_boxplot() +
  ggtitle('Boxplot of mpg')

This produces the following boxplot:

The y-axis displays the distribution of values for the mpg variable.

Notice that the boxplot currently takes up the entire width of the x-axis.

To make this width thinner, we can use the following syntax:

library(ggplot2)

#create boxplot of mpg with thinner width
ggplot(mtcars, aes(y=mpg)) +
  geom_boxplot(width=0.5) +
  xlim(-1,1)

This produces the following boxplot:

geom_boxplot width in R

Notice that the width of the boxplot is much thinner now.

Feel free to play around with the values for width and xlim() to create a boxplot with the exact width that you would like.

Additional Resources

The following tutorials explain how to perform other common tasks in ggplot2:

How to Create a Density Plot with ggplot2
How to Adjust Space Between Bars in ggplot2
How to Remove NAs from Plot in ggplot2
How to Change Colors of Bars in Stacked Bart Chart in ggplot2

Leave a Reply

Your email address will not be published. Required fields are marked *