2.2 Fuel economy data
In this chapter, we’ll mostly use one data set that’s bundled with ggplot2:
mpg. It includes information about the fuel economy of popular car models in 1999 and 2008, collected by the US Environmental Protection Agency, http://fueleconomy.gov. You can access the data by loading ggplot2:
library(ggplot2) mpg#> # A tibble: 234 x 11 #> manufacturer model displ year cyl trans drv cty hwy fl class #> <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr> #> 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compa… #> 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compa… #> 3 audi a4 2 2008 4 manual(m6) f 20 31 p compa… #> 4 audi a4 2 2008 4 auto(av) f 21 30 p compa… #> 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compa… #> 6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compa… #> # … with 228 more rows
The variables are mostly self-explanatory:
hwyrecord miles per gallon (mpg) for city and highway driving.
displis the engine displacement in litres.
drvis the drivetrain: front wheel (f), rear wheel (r) or four wheel (4).
modelis the model of car. There are 38 models, selected because they had a new edition every year between 1999 and 2008.
classis a categorical variable describing the “type” of car: two seater, SUV, compact, etc.
This dataset suggests many interesting questions. How are engine size and fuel economy related? Do certain manufacturers care more about fuel economy than others? Has fuel economy improved in the last ten years? We will try to answer some of these questions, and in the process learn how to create some basic plots with ggplot2.
List five functions that you could use to get more information about the
How can you find out what other datasets are included with ggplot2?
Apart from the US, most countries use fuel consumption (fuel consumed over fixed distance) rather than fuel economy (distance travelled with fixed amount of fuel). How could you convert
hwyinto the European standard of l/100km?
Which manufacturer has the most models in this dataset? Which model has the most variations? Does your answer change if you remove the redundant specification of drive train (e.g. “pathfinder 4wd”, “a4 quattro”) from the model name?