Question #2: Regarding the fuel type variable, the value “d” represents diesel, “p” represents premium (petrol) and “r” represents regular (petrol). Do you think there is an effect of fuel type on how many miles a vehicle can run on average per gallon of fuel?

ggplot(data = mpg) + 
  geom_boxplot(mapping = aes(x = fl, y = hwy)) + 
  xlab("Fuel Type") + 
  ylab("Miles Per Gallon in Highway") + 
  ggtitle("Fuel Economy (Highway) vs Fuel Type") + 
  theme(plot.title = element_text(hjust = 0.5))

ggplot(data = mpg) + 
  geom_boxplot(mapping = aes(x = fl, y = cty)) + 
  xlab("Fuel Type") + ylab("Miles Per Gallon in City") + 
  ggtitle("Fuel Economy (City) vs Fuel Type") + 
  theme(plot.title = element_text(hjust = 0.5))

Answer:

According to the figure, the fuel type seems to have certain effect on the miles per gallon a car can drive. In which, “d” diesel and “c” reveal larger miles per gallon while “e” shows the least fuel efficiency in miles to drive per gallon, in both city and highway

Question #3:Do you think there is a difference in fuel economy for vehicles made in 1999 and 2008? (When plotting with “year” variable, use as.factor(year) to convert it to categorical variables. This will be explained in future classes.)

ggplot(data = mpg, mapping = aes(x = as.factor(year), y = cty)) +
  stat_boxplot(geom = "errorbar", width = 0.5) +
  geom_boxplot() +
  labs(x = "Year", y = "Miles Per Gallon in City", title = "Fuel Economy (City) Between 1999 and 2008") + 
  theme(plot.title = element_text(hjust = 0.5))

Answer:

According to the above figure, there is no significant difference in fuel economy between vehicles made in 1999 and 2008

Question #4: What happens if you make a scatter plot of class vs drv? Do you think this plot is useful or not?

ggplot(mpg) +
  geom_point(mapping = aes(x = class, y = drv)) +
  labs(x = "Type of car", y = "Type of drive train") +
  theme(plot.title = element_text(hjust = 0.5))

Answer:

Although the plot is scattering without showing a concrete pattern, it is still useful to identify the according drive train for each car type. For example, 2seater cars are all rear wheel drive while minivans are all front-wheel drive.