Creating a conditional histogram

A histogram is one of the simplest ways to visualize the univariate distribution. Sometimes, we need to produce a histogram for each group in order to compare the distribution in a subgroup of another categorical variable. In this recipe, we will see how we can create a conditional histogram using the lattice functionality.

To visualize a conditional histogram, we need at least one numeric variable and one categorical variable. We have all this information in the dataset that we have simulated in earlier recipes. Here, we will reproduce the same cross-tabulation raw data with the following code:

We want to produce a histogram for each value of the sex variable; for this, take a look at this simple code with the lattice implementation:

How to do it…

In the histogram command, the most important part of the formula is the name of the variable just after the ~ symbol. We need to write the name of the variable for which we want to create a histogram just after the ~ symbol. For a conditional histogram, we need to provide the input of that variable after the vertical bar symbol ( | ). The default value for the y axis is the percentage for each of the interval in x-axis, but we can change this as per our need. Here, we have used density for the y axis values, which actually calculated probability density values instead of frequency counts.

If we want to apply conditions on multiple categorical variables, then we can do this by simply adding the additional variable name using a plus (+) sign, as shown in the following code:

There's more…

To plot more than one numeric variable, we just need to add the new variable as shown in the following line of code:

There's more…

To annotate the histogram, we can use main, xlab, and ylab, as we previously did for bar charts.