Creating box plots with narrow boxes for a small number of variables

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.

Getting ready

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")

How to do it...

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)
How to do it...

Let's improve the look of the graph by making the boxes narrower:

boxplot(air,boxwex=0.2,las=1)
How to do it...

How it works...

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

Note that we must not close the graphics device if we want to retain the setting. If we do close the device, we will need to set las to 1 again either using the par() function call or within each boxplot() function call. From now on, it is assumed that we will set las to 1 globally.

There's more

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))

See also

Setting arbitrarily different widths for boxes using the width argument is not a good idea unless the difference in widths conveys another important fact about the data. We will see one such example later in the chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.123.182