Session Ready
Exercise

A ggplot2 graphics example

This final exercise provides an introduction to the ggplot2 graphics package. Like the lattice package, the ggplot2 package is also based on grid graphics and it also represents a general purpose graphics package.

The unique feature of the ggplot2 package is that it is based on the grammar of graphics, a systematic approach to building and modifying graphical displays, starting with a basic graphics element and refining it through the addition of successive modifiers.

This exercise provides a simple illustration of the approach. Specifically, you are asked to use ggplot2 to, first, create a simple scatterplot of MPG.city versus Horsepower from the Cars93 data frame. Next, you are asked to add a simple modifier that adds color based on the value of the Cylinders variable. Finally, you are asked to convert this result into a colored bubble plot, with both bubble sizes and colors determined by the Cylinders variable.

The primary purpose of this exercise is to give you a flavor of the ggplot2 package. DataCamp offers several courses dedicated to using this popular graphics package to generate high-quality data visualizations.

Instructions
100 XP

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

  • Load the ggplot2 package.
  • Create basePlot as the ggplot object based on the Cars93 data frame from the MASS package and the scatterplot aesthetic with x as the Horsepower variable and y as the MPG.city variable. When passing the variable names to x and y, they should be unquoted (e.g. x = var_name). (Note that this definition does not render the scatterplot.)
  • Make a simple rendering of basePlot with the geom_point() function.
  • Make a second rendering using the color parameter of geom_point() to specify the first six Iliinsky-Steele colors for the six Cylinders levels.
  • Make a third rendering with the same coloring as the second one, but with the point sizes corresponding to the levels of the Cylinders factor variable.