Session Ready
Exercise

Superposition in density plots

Just like conditioning variables, grouping variables also split up the data into different subgroups. However, instead of being juxtaposed in different panels, these subgroups are superposed within the same panel, and differentiated from each other with different graphical parameters such as color.

You have already seen how a grouping variable can be specified explicitly as the groups argument. It is also possible to create a grouping implicitly by specifying the variables to be grouped in the formula and separating them by a plus, along with the argument outer = FALSE. This is similar to conditioning implicitly, which requires outer = TRUE.

For example, in a call to densityplot(), you could use

 ~ x1 + x2, outer = TRUE

to plot x1 and x2 in separate panels, and use

 ~ x1 + x2, outer = FALSE

to plot x1 and x2 within the same panels as different groups.

You have previously used the first form to compare density plots of rate.male and rate.female in the USCancerRates dataset in different panels. Your goal for this exercise is to plot them as groups within the same panel.

Instructions
100 XP

The USCancerRates dataset has been pre-loaded.

  • Create kernel density plots for the variables rate.male and rate.female.

  • Use the outer argument to ensure that the density estimates are superposed within the same panel.

  • Use the xlab argument to set the x-axis label to "Rate (per 100,000)".

  • Set the auto.key argument to true to display a legend.

The other arguments plot.points and ref are used to suppress plotting the data points and add a reference line, respectively, and should remain unchanged.