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

Setting the bin size and the number of breaks


As we saw in the previous recipe, the hist() function automatically computes the number of breaks and the size of bins in which the values of the variable will be grouped. In this recipe, we will learn how we can control this and specify exactly how many bins we want or where to have breaks between bars.

Getting ready

Once again, we will use the airpollution.csv example dataset, so make sure that you have loaded it:

air<-read.csv("airpollution.csv")

How to do it...

First, let's see how to specify the number of breaks. Let's make 20 breaks in the nitrogen oxides histogram instead of the default 11:

hist(air$Nitrogen.Oxides,
breaks=20,xlab="Nitrogen Oxide Concentrations",
main="Distribution of Nitrogen Oxide Concentrations")

How it works...

We used the breaks argument to specify the number of bars for the histogram. We set breaks to 20. However, the graph shows more than 20 bars because R uses the value specified only as a suggestion and computes the...