How to Use Data in Tall Format in Lattice Plots in R

When you have data in tall format in R, you can easily use lattice graphics to visualize subgroups in your data. For instance, what happens when you want to analyze more than one variable simultaneously?

Consider the built-in dataset longley, containing data about employment, unemployment, and other population indicators:

One way to easily analyze the different variables of a data frame is to first reshape the data frame from wide format to tall format.

A wide data frame contains a column for each variable. A tall data frame contains all the same information, but the data is organized in such a way that one column is reserved for identifying the name of the variable and a second column contains the actual data.

An easy way to reshape a data frame from wide format to tall format is to use the melt() function in the reshape2 package. Remember:reshape2 is not part of base R — it’s an add-on package that is available on CRAN. You can install it with the install.packages("reshape2") function.

The additional arguments par.strip.text and scales control the font size (character expansion ratio) of the strip at the top of the chart, as well as the scale.

When you create plots with multiple groups, make sure that the resulting plot is meaningful. For example, the unit of GNP (short for Gross National Product) is probably billions of dollars. In contrast the unit of population is probably millions of people. (The documentation of the longley dataset is not clear on this topic.)

Be very careful when you present plots like this — you don’t want to be accused of creating chart junk (misleading graphics).