R automatically adjusts the widths of boxes in a box plot according to the number of variables. This works fine when we have a relatively large number of variables (more than 4), but you might find that for a small number of variables, the default boxes are too wide. In this recipe, we will learn how to make the boxes narrower.
We are only using the base graphics functions for this recipe. So, just open up the R prompt and type in the following code. We will use the airpollution.csv
example dataset for this recipe. So, let's first load it:
air<-read.csv("airpollution.csv")
We want to make a box plot summarizing the two columns in our dataset: respirable particles and nitrogen oxides. If we simply use the boxplot
command, we get a box plot with very wide boxes:
boxplot(air,las=1)
Let's improve the look of the graph by making the boxes narrower:
boxplot(air,boxwex=0.2,las=1)
So, we changed the width of the boxes by passing the boxwex
argument to the boxplot()
command. We set boxwex
to a value of 0.2
. The value depends on the number of variables we are plotting, but it should usually be less than one.
Note that we also passed the las
argument with a value of 1
to make the y axis labels horizontal. By default, they are parallel to the y axis, thus making them difficult to read. As we want this setting in all our graphs, we can set it globally by calling the par()
function:
par(las=1)
Note that when we specify a width using boxwex
, the same value is applied to all the boxes in the plot. There is another argument, width
, which can be used to set the relative widths of boxes. The width
argument takes values in the form of a vector containing a value for each box. For example, if we want the box for respirable particles twice as wide as nitrogen oxides, we will run the following line of code:
boxplot(air,width=c(1,2))
18.116.42.137