Book Image

R Graphs Cookbook Second Edition

Book Image

R Graphs Cookbook Second Edition

Overview of this book

Table of Contents (22 chapters)
R Graphs Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

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)

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

boxplot(air,boxwex=0.2,las=1)

How it works...

So, we changed...