--- title: "Precept 5" author: "Emily Nelson" date: "March 2, 2016" output: html_document --- #Fun With `ggplot2` and Alcohol ```{r setup, message=FALSE} library(dplyr) library(ggplot2) load("wine.RData") wine <- wine %>% tbl_df() ``` #Boxplots / Violin Plots What does a boxplot / violin plot show us? 1. Show a boxplot of the pH for each quality score. (Why do we have to cast?) 2. Add a `color` layer for wine type. What happens? What if you use `fill` instead? 3. Change the colors manually. (What if you wanted to change the `fill` colors manually?) 4. Spruce up the axis labels and title. 5. Change the `theme`. 6. Change it to a violin plot. What is the difference? ```{r boxplots} ``` #Histograms What does a histogram show us? 1. Make a histogram of the alcohol content of the wines with appropirate axis labels. (What happens if I give `geom_histogram` both an `x` and a `y` value?) 2. Add a `fill` layer based on type with manual color selection. What happens? 3. Add a vertical line showing the mean of the data. (How would you add a horizontal line?) ```{r histograms} ``` #Barplots What does a barplot show us? 1. Make a barplot showing the mean quality scores for high and low alcohol content wines. Use appropriate themes, labels, etc. 2. Add a color layer. What happens? Is this a good way to visualize the information? 3. Make it better. ```{r barplots} wine %>% mutate(alcohol.amt = ifelse(alcohol > mean(wine$alcohol), "high", "low")) %>% group_by(type, alcohol.amt) %>% summarize(mean.quality = mean(quality)) ``` #Scatter Plots What does a scatter plot show us? 1. Make a scatter plot showing the relationship between the pH and the fixed acidity, using an appropriate theme and labels. 2. Add a trend line using `stat_smooth`. Try both `loess` and `lm` types. How are they different? 3. Add a color layer for the type of wine. What does this do? What happened to the trend line? 4. Make the points transparent since there is a lot of overlap. ```{r scatterplots} ``` #Facets The above plot still doesn't look great. There is too much overlap. We can fix this with facets! 1. Add a facet layer. What should it be? 2. Change the color of the trend line. 3. Free the x and y scales. What happens? ```{r facets} ``` #Hexbin Change the above plot to a `hexbin` plot. Note: may need to install the `hexbin` package. ```{r hexbin} ```