Skip to content

Instantly share code, notes, and snippets.

@ateucher
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ateucher/d50981c611d51dd2783e to your computer and use it in GitHub Desktop.
Save ateucher/d50981c611d51dd2783e to your computer and use it in GitHub Desktop.
Compare fuel economy of cars
library(fueleconomy)
library(dplyr)
library(tidyr)
library(ggplot2)
comp_vehicles <- vehicles %>%
filter(cyl == 4,
grepl("Utility", class),
make %in% c("Honda", "Toyota","Subaru",
"Hyundai", "Kia", "Mazda",
"Mitsubishi", "Nissan"),
year > 2007,
year < 2014,
grepl("All-Wheel|4-Wheel", drive)) %>%
mutate(transmission = ifelse(grepl("Auto", trans), "Automatic", "Manual")
) %>%
gather(mpg_type, mpg, cty, hwy)
ggplot(comp_vehicles, aes(x = mpg, y = reorder(paste(make, model), mpg, max),
colour = transmission)) +
facet_grid(mpg_type ~ year, scales = "free_y") +
geom_point(aes(size = displ)) + scale_size(range = c(2,4)) +
labs(x = "Miles Per Gallon", y = "Make / Model", size = "Engine displacement",
colour = "Transmission")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment