Visualizing distributions as count frequencies or probability densities

Histograms can represent the distribution of values either as frequency (the absolute number of times values fall within specific ranges) or as probability density (the proportion of the values that falls within specific ranges). In this recipe, we will learn how to choose one or the other.

Getting ready

We are only using 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 will use the hist() base graphics function to make our histogram, first showing the frequency and then probability density of nitrogen oxide concentrations:

hist(air$Nitrogen.Oxides,
xlab="Nitrogen Oxide Concentrations",
main="Distribution of Nitrogen Oxide Concentrations")
How to do it...

Now, let's make the same histogram but with probability instead of frequency:

hist(air$Nitrogen.Oxides, freq=FALSE,
xlab="Nitrogen Oxide Concentrations",
main="Distribution of Nitrogen Oxide Concentrations")
How to do it...

How it works...

The first example, which shows the frequency counts of different value ranges of nitrogen oxides, simply uses a call to the hist() function in the base graphics library. The variable is passed as the first argument; by default, the histogram plotted shows frequency. In the second example, we pass an extra freq argument and set it to FALSE, which results in a histogram that shows probability densities. This suggests that, by default, freq is set to TRUE. The help section on hist() (?hist) states that freq defaults to TRUE if, and only if, the breaks are equidistant and the probability is not specified.

There's more

An alternative to using the freq argument is the prob argument that, as the name suggests, takes the opposite value to freq. So, by default, it is set to FALSE; if we want to show probability densities, then we need to set prob to TRUE.

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

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