How to do it...

This recipe will investigate the makes and models of automobiles and how they have changed over time:

  1. Let's look at how the makes and models of cars inform fuel efficiency over time. First, let's look at the frequency of the makes and models of cars available in the US over this time and concentrate on four-cylinder cars:
carsMake <- ddply(gasCars4, ~year, summarise, numberOfMakes = length(unique(make))) 

ggplot(carsMake, aes(year, numberOfMakes)) + geom_point() + labs(x = "Year", y = "Number of available makes") + ggtitle("Four cylinder cars")

We see in the following graph that there has been a decline in the number of makes available over this period, though there has been a small uptick in recent times.

  1. Can we look at the makes that have been available for every year of this study? We find that there are only 12 manufactures that made four-cylinder cars every year during this period:
uniqMakes <- dlply(gasCars4, ~year, function(x) 
unique(x$make))

commonMakes <- Reduce(intersect, uniqMakes)
commonMakes
## [1] "Ford" "Honda" "Toyota" "Volkswagen"
"Chevrolet"
## [6] "Chrysler" "Nissan" "Dodge" "Mazda"
"Mitsubishi"
## [11] "Subaru" "Jeep"
  1. How have these manufacturers done over time with respect to fuel efficiency? We find that most manufacturers have shown improvement over this time, though several manufacturers have demonstrated quite sharp fuel efficiency increases in the last five years:
carsCommonMakes4 <- subset(gasCars4, make %in% commonMakes) 
avgMPG_commonMakes <- ddply(carsCommonMakes4, ~year + make,
summarise, avgMPG = mean(comb08))


ggplot(avgMPG_commonMakes, aes(year, avgMPG)) + geom_line() +
facet_wrap(~make, nrow = 3)

The preceding commands will give you the following plot: