--- title: "Explore and Visualize" author: "Prof. Eric A. Suess" output: word_document: default html_document: df_print: paged pdf_document: default html_notebook: default --- # Chapter 3 Data Visualization 1. ggplot2 2. scatterplots, points, color, shape 3. faceting Today we are going to try some of the code from Chapter 3 Data Visualization. To start we will load the tidyverse. Note that *ggplot2* is the first package loaded! ```{r message=FALSE} library(tidyverse) ``` We will start the *mpg* dataset that is in the ggplot2 package. ```{r} mpg ``` Make a scatterplot of highway miles per gallon (hwy) and engine size (displ). Start with an empty graph and add to it. ```{r} ggplot(data = mpg) ``` Add points using geom_point mapping and aesthetic. ```{r} ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) ``` Add color. ```{r} ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = class)) ``` Change size of the points. ```{r} ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, size = class)) ``` Change the transparentcy or shape. ```{r} # Left ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, alpha = class)) # Right ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, shape = class)) ``` Specify color. ```{r} ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy), color = "blue") ``` Faceting, spliting a plot into separate plots for different levels of a categorical variable. ```{r} ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) + facet_wrap(~ class, nrow = 2) ```