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

Get R: Data Analysis and Visualization now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.