Calvin Chin [calvin3663@hotmail.com]
Jan 2016
Introduction
This is a Shiny application that shows the average mileage for vehicles manufactured between 1999 - 2008.
Source data is obtained from the R built-in mpg dataset included in the ggplot2 package.
library(ggplot2)
knitr::kable(head(mpg))
| manufacturer | model | displ | year | cyl | trans | drv | cty | hwy | fl | class |
|---|---|---|---|---|---|---|---|---|---|---|
| audi | a4 | 1.8 | 1999 | 4 | auto(l5) | f | 18 | 29 | p | compact |
| audi | a4 | 1.8 | 1999 | 4 | manual(m5) | f | 21 | 29 | p | compact |
| audi | a4 | 2.0 | 2008 | 4 | manual(m6) | f | 20 | 31 | p | compact |
| audi | a4 | 2.0 | 2008 | 4 | auto(av) | f | 21 | 30 | p | compact |
| audi | a4 | 2.8 | 1999 | 6 | auto(l5) | f | 16 | 26 | p | compact |
| audi | a4 | 2.8 | 1999 | 6 | manual(m5) | f | 18 | 26 | p | compact |
p1 <- ggplot(mpg, aes(x=class, y=cty, fill=class)) + stat_summary(fun.y=mean, geom="bar") + scale_y_discrete("Avg MPG (City)")
p2 <- ggplot(mpg, aes(x=class, y=hwy, fill=class)) + stat_summary(fun.y=mean, geom="bar") + scale_y_discrete("Avg MPG (Highway)")
df1 <- aggregate(x=list(Avg.Mileage.Cty=mpg$cty), by=list(Class=mpg$class), FUN=mean)
df2 <- aggregate(x=list(Avg.Mileage.Hwy=mpg$hwy), by=list(Class=mpg$class), FUN=mean)
df_merge <- merge(x = df1, y = df2, by="Class", all=TRUE)
| Class | Avg.Mileage.Cty | Avg.Mileage.Hwy |
|---|---|---|
| 2seater | 15.40000 | 24.80000 |
| compact | 20.12766 | 28.29787 |
| midsize | 18.75610 | 27.29268 |
| minivan | 15.81818 | 22.36364 |
| pickup | 13.00000 | 16.87879 |
| subcompact | 20.37143 | 28.14286 |
| suv | 13.50000 | 18.12903 |
To run the application, use your browser to open the link below:
https://calvin3663.shinyapps.io/coursera-module-9-assignment/