Session Ready
Exercise

A lattice graphics example

Another package built on grid graphics is lattice, but unlike tabplot, lattice is a general-purpose graphics package that provides alternative implementations of many of the plotting functions available in base graphics. Specific examples include scatterplots with the xyplot() function, bar charts with the barchart() function, and boxplots with the bwplot() function.

One important difference between lattice graphics and base graphics is that similar functions available in both graphics systems often produce very different results when applied to the same data. As a specific example, the bwplot() function creates horizontal boxplots, while the default result of the boxplot() is a vertical boxplot display.

Another more important difference between lattice and base graphics is that lattice graphics supports conditional plots that show the separate relationships between variables within different groups. This capability is illustrated in this exercise, where you are asked to construct a plot showing the relationship between the variables calories and sugars from the UScereal data frame, conditional on the value of the shelf variable.

Instructions
100 XP
  • Load the lattice package to make the function xyplot() available.
  • Create the formula of calories vs. sugars conditional on shelf from the UScereal data frame in the MASS package. It takes the form y ~ x | z. Assign this as calories_vs_sugars_by_shelf.
  • Draw a conditional scatterplot using xyplot(), passing the formula and the dataset.