Title: One Continuous Variable and One Categorical Variable
Course: UGQuants (SSPS) > Section: Data Visualisation

One Continuous Variable and One Categorical Variable

Where one variable (typically the one we would consider dependent in any relationship) is continuous and the other is categorical, it is possible to extend the histogram and box plots used to understand the distribution of a single variable to consider whether values vary between different groups of cases.

Let’s return to the example of CO2 emissions of the different countries which was considered in the visualisation for single variable section earlier in this module.  It would seem reasonable to expect that CO2 emissions will vary between countries depending on their level of economic development.  The WDI2010 dataset includes a categorical variable breaking countries into different groups based on national income per capita.  A useful insight into how CO2 emissions vary depending on the wealth of a nation can therefore be developed by creating separate histograms or boxplots for each group of nations.

The figure below shows a set of histograms for CO2 emissions in each of the 5 income groups present in the data.  Can you identify any patterns?

Figure 20: CO2 emissions by income group (histogram)

Source: WDI 2011.

It can be difficult to draw direct comparisons between the different groups, a problem which becomes greater if you are trying to compare groups which are not adjacent to each other.  In addition, some software packages will, by default, give each histogram bin sizes and y-axis labels to best represent the cases within each group.  This lack of consistency means comparisons are not intuitive, and indeed if the reader fails to notice the difference in scales, potentially highly misleading.

A better understanding of how the distribution of CO2 emissions varies by income group can be achieved by using the “density plot” version of the histogram and overlaying all the plots onto a single set of axes (here is the syntax).  Since the scales associated with each distribution are now identical, and the different distributions are now closer together, it is now much easier to draw conclusions – that in general CO2 emissions seem to increase with national income.

Figure 21: CO2 emissions by income group (density plot)

Source: WDI 2011.

As the number of distributions increases, it can become increasingly difficult to distinguish one distribution from another on an overlapping graph like that above.  The inclusion of five different distributions on a single graph is probably towards the upper limit of what you would want to include on a single graph.

Just as with histograms, it is possible to create multiple box plots for different groups of cases and display them in a single visualisation.  The more complex layout of a boxplot means that overlaying them (as done with the density plots above) is not appropriate.  Instead, these should be presented side by side (similar to the histograms).  However, providing the Y-axis of each box plot are identical (and most statistical software has an option to create such graphs) then the more detailed nature of a boxplot (with clear lines for the median value etc) allows for comparisons to be made with relative ease (much more so than with side by side histograms).  The ease with which patterns can be identified is also dependent on the order of the values of the x-axis.  Where the categorical variable is ordinal (as is the case in the CO2 and income example) it is sensible to order the different boxplots to reflect the ordering of the ordinal variable (i.e. from high income to low).  Where the categorical variable is nominal in nature (i.e the categories have no discernible order), it can prove useful to order the boxplots in order of the median value they represent from lowest to highest or vice versa.

Figure 22: CO2 emissions by income group (boxplots)

Source: WDI 2011.