## Learning objectives Week 3 Day 2:

- `ggplot`; aesthetics and geoms
- plotting distributions, densities, histograms

First, let's load some data about wine.
(Data source)[https://archive.ics.uci.edu/ml/datasets/Wine+Quality]

```{r}
library(readr)
winequality_red <- read_csv("classes/week3/winequality-red.csv")
head(winequality_red)
```

That didn't work! Can you load in the data properly?

```{r echo=TRUE}
# student code:



```

(From winefolly.com) "It’s a little known fact that the world’s largest wine producer, E. J.Gallo, built their empire off the success of their white wine called Thunderbird. The wine was originally designed to appeal to a young market and has now garnered cult status as a “bum wine.” Why was Thunderbird such a success? Well, simply put it has 20% alcohol by volume (ABV)."

Take a moment to check the dimensions, structure, and layout of the data. Talk about this with your partner. How would you describe the data to an audience? 

```{r eval=FALSE, include=FALSE}
# student code:
# what are the dimensions of the data?




```

First, let's make a histogram. A histogram is a plot that uses bars to show the frequency distribution of a set of continuous data or an item of data in successive intervals. Let's focus on the 'alcohol' variable for exploring a histogram.

```{r}
library(ggplot2)
ggplot(winequality_red, aes(alcohol)) +
  geom_histogram()
```

In ggplot when you make a histogram, the default is 'bins = 30'. We can use ‘binwidth’ to set how wide each bin is. 'binwidth' overrides 'bins'. Alternatively, we can use 'breaks' and 'seq' to set bin width. But in that case you should do a summary statistics first on the variable of interest to know its extreme values. 

Play with the graph below to alter the binwidth. Look up binwidth online, get familiar with it! With your partner, figure out what 'seq' function does. Read this thread about the differences between binwidth and breaks: https://stackoverflow.com/questions/32102608/r-hist-vs-geom-hist-break-points

```{r}
# binwidth example
ggplot(winequality_red, aes(alcohol)) +
  geom_histogram(binwidth = .5)

#use summary to see extreme values and then use breaks
summary(winequality_red$alcohol)
ggplot(winequality_red, aes(alcohol)) +
  geom_histogram(breaks = seq(8, 15, by = 0.5))
```

To explore distributions of a single variable, sometimes we might want to think about the density curve. A density curve is a graph that shows probability. The area under the curve is equal to 100 percent of all probabilities. Usually represented as decimals, we can also say the area is equal to 1 (100% as a decimal of 1). 

Make a density plot:

```{r}
ggplot(winequality_red, aes(alcohol)) +
  geom_density()
```

Combine the two layers on one graphic to create a histogram with density curve overlay. Note: In the ggplot2 package, the double dots approach will be replaced by the calc() function. Instead of using `..density..` to indicate that you want to use the density of the inherited aesthetic, you can use stat(density).

```{r}
ggplot(winequality_red, aes(x = alcohol)) +
  geom_histogram(aes(y = ..density..), colour = "black", fill = "white") +
  geom_density(alpha = .2, fill="#FF6655") #overlay with a transparent (alpha value) density plot
```

Additional lines like “mean” can be added to existing plots using geom_vline. See how this is an additional layer, and is nice for highlighting this statistical parameter of the data.

```{r}
ggplot(winequality_red, aes(x = alcohol)) +
  geom_histogram(aes(y = ..density..), # the histogram will display "density" on its y-axis
                 binwidth = .5, colour = "blue", fill = "white") +
  geom_density(alpha = .2, fill="#FF6655") +
  geom_vline(aes(xintercept = mean(alcohol, na.rm = T)),
             colour = "gray", linetype ="longdash", size = .8)
```

In cases where the variable has a long tail, you may want to do a log transformation on the variable so that its histogram and density plot looks normal. 

```{r}
#student code
#plot the histogram and density plot of variable 'chlorides'



#plot the histogram and density plot of the log-transformed variable log(chlorides)
#hint: + scale_x_log10()
```


## Bivariate relationships review + distributions

Let's apply lessons from last class and explore some relationships in our 'red wine' data to examine where there is relationship between quality ratings and alcohol content. Below, plot a scatterplot of wine quality vs. alcohol content.

```{r}
# student code:


```

Oh snap! This looks weird, why? Because we have stacked values for each quality. We can apply some random noise (jitter) to help us understand how many data points fall into each category.

```{r}
# student code: 


```

This is a nice start, as it quickly tells us that most of the wines in our data are of medium level quality, around 10% alcohol. 

What if we also wanted to analyze how `fixed acidity` fits into the wine quality picture? Can you add this to your plot? This means that we are going to add a third variable to our graphic. Importantly, this variable can be mapped onto the points to express variation across points in the data on this third feature. 

Bonus: you can read about and toy with the `viridis` package to play with color settings. Also apply label and theme layers.

```{r}

library(viridis)
library(RColorBrewer)

# plot quality (x) against (y), use jitter, set color to 'fixed acidity'



# you could also play around with sulphates, here we play with size which looks bad
ggplot(winequality_red, aes(x=quality, y= alcohol)) +
      geom_jitter(aes(size=sulphates))
```

## In class lab: Nashville schools' data

On your own, in groups, who can make the best graphics? We will project a graphic (or several) at the end of class if time or next class!!

```{r}
metro_nash_schools <- read.csv("metro-nash-schools.csv")
```


1. Load this data
2. Explore this data, what is its structure?
3. Begin exploring this data further using only histograms, density plots, and scatterplots--make one of each, we will come around to see your graphics.

Instructor example:

```{r}
#dimension, structure, and first few lines of dataset


#histogram and density plot for number of female students in each school



#density plot of number of disability students



#scatterplot of number of students with limited English proficiency vs number of students who are economically disadvantaged



```

---

## References

-http://rstudio-pubs-static.s3.amazonaws.com/374857_5a23bad9783a43c1b102aa80aa5c1a7c.html -https://stackoverflow.com/questions/17502808/double-dots-in-a-ggplot
-https://www.datacamp.com/community/tutorials/make-histogram-ggplot2