Session Ready
Exercise

Using color to enhance stacked barplots

The most common barplot consists of a collection of vertical bars, each representing some characteristic of one of a finite number of datasets or data subsets. Several previous exercises have illustrated the utility of the somewhat less common horizontal barplot, useful in part because it allows text to be displayed horizontally across the bars, as illustrated in Exercise 1.

Another useful variant of the standard bar plot is the stacked bar plot, where each bar in the plot is partitioned into segments characterizing portions of the data characterized by the bar. Stacked bar plots can also be generated using the barplot() function. Here, each bar is specified by a matrix whose columns specify the heights of the segments in each bar.

By default, the barplot() function generates stacked bar plots using different shades of gray for the different segments of each bar in the plot. The point of this exercise is to show that, if we can use it, color can be a more effective alternative.

Instructions
100 XP

The character vector IScolors from the previous exercises is still available in your workspace.

  • Create the table tbl giving the record counts for each Cylinders value at each Origin value in the Cars93 dataset (this table should have 6 rows and 2 columns).
  • Using the barplot() function, create a stacked barplot that summarizes this information using shades of gray.
  • Recreate this first plot, but using the first six Iliinsky and Steele colors for the six Cylinders levels.