--- output: knitrBootstrap::bootstrap_document: theme.chooser: TRUE highlight.chooser: TRUE --- # Cars example # From [statmethods.net](http://www.statmethods.net/advgraphs/ggplot2.html). ```{r cars_setup} # ggplot2 examples library(ggplot2) #use color brewer as default discrete colors scale_colour_discrete <- function(...) scale_color_brewer(palette="Set1", ...) scale_fill_discrete <- function(...) scale_fill_brewer(palette="Set1", ...) data('mtcars') # create factors with value labels mtcars$gear <- factor(mtcars$gear,levels=c(3,4,5), labels=c("3gears","4gears","5gears")) mtcars$am <- factor(mtcars$am,levels=c(0,1), labels=c("Automatic","Manual")) mtcars$cyl <- factor(mtcars$cyl,levels=c(4,6,8), labels=c("4cyl","6cyl","8cyl")) head(mtcars) ``` ## Kernel density plots for mpg ## Grouped by number of gears (indicated by color). ```{r cars_density, dev='png', warning=FALSE} qplot(mpg, data=mtcars, geom="density", fill=gear, alpha=I(.5), main="Distribution of Gas Milage", xlab="Miles Per Gallon", ylab="Density") ``` ## Scatterplot of mpg vs. hp ## For each combination of gears and cylinders in each facet, transmission type is represented by shape and color. ```{r cars_scatter, dev='png', warning=FALSE} qplot(hp, mpg, data=mtcars, shape=am, color=am, facets=gear~cyl, size=I(3), xlab="Horsepower", ylab="Miles per Gallon") ``` ## Regressions of mpg on weight ## Separate for each number of cylinders. ```{r cars_regressions, dev='png', warning=FALSE} ggplot(mtcars, aes(wt, mpg, color = cyl)) + geom_point() + geom_smooth(method = "lm", formula = y~x) + labs(title = "Regression of MPG on Weight", x = "Weight", y = "Miles per Gallon") ``` ## Boxplots of mpg by number of gears ## Observations (points) are overlayed and jittered. ```{r cars_boxplots, dev='png', warning=FALSE} qplot(gear, mpg, data=mtcars, geom=c("boxplot", "jitter"), fill=gear, main="Mileage by Gear Number", xlab="", ylab="Miles per Gallon") ``` Author: [Jim Hester](http://jimhester.com) Created: 2013 Mar 20 10:57:07 AM Last Modified: 2014 Apr 17 04:53:11 PM