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.
Once again, we will use the airpollution.csv
example dataset, so make sure that you have loaded it:
air<-read.csv("airpollution.csv")
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")