Session Ready
Exercise

The surprising utility of the type "n" option

The type = "n" option was discussed in the video and this exercise provides a simple example. This option is especially useful when we are plotting data from multiple sources on a common set of axes. In such cases, we can compute ranges for the x- and y-axes so that all data points will appear on the plot, and then add the data with subsequent calls to points() or lines() as appropriate.

This exercise asks you to generate a plot that compares mileage vs. horsepower data from two different sources: the mtcars data frame in the datasets package and the Cars93 data frame in the MASS package. To distinguish the different results from these data sources, the graphics parameter pch is used to specify point shapes. See ?points for a comprehensive list of some pch values and their corresponding point shapes.

Instructions
100 XP
  • Compute max_hp as the maximum of Horsepower from the Cars93 data frame and hp from the mtcars data frame.
  • Compute max_mpg as the maximum of MPG.city from the Cars93 data frame, MPG.highway from this data frame, and mpg from the mtcars data frame.
  • Using the type = "n" option, set up a plot with an x-axis that runs from zero to max_hp and a y-axis that runs from zero to max_mpg, with labels Horsepower and Miles per gallon.
  • Using the points() function, add mpg vs. hp from the mtcars data frame to the plot as open circles (pch = 1).
  • Using the points() function, add MPG.city vs. Horsepower to the plot as solid squares (refer to the plot of pch values).
  • Using the points() function, add MPG.highway vs. Horsepower to the plot as open triangles pointing upwards (refer to the plot of pch values).