---
title: "Lesson 07 - Creating graphics (Notes)"
author: "PUT YOUR NAME HERE"
output: html_document
---

# Introduction

```{r, echo=FALSE, warning=FALSE, message=FALSE}
knitr::opts_chunk$set(warning=FALSE, message=FALSE,error=TRUE) 
library(ggplot2)
library(dplyr)
diamonds <- ggplot2::diamonds
set.seed(1410) # Make the sample reproducible
dsmall <- diamonds[sample(nrow(diamonds), 1000), ] # pull a quasi-random sample of 1000 records
```

Follow along during lecture to create the plots that are discussed. The sections in here align with the lecture material. 
For each code chunk provided, fill in the blanks (_____) to complete the code to create the plot by pressing the green "play" button to run that specific code chunk. That is, you don't have to knit after every code chunk.


# Univariate (One Variable)


## Categorical variables

### Tables
A table of `cut`. 

```{r}
 
```


### Barcharts / Barplots
Fill in the blank line to create a barchart of `cut`. 

```{r}
ggplot(dsmall, aes(x=____)) + geom_bar()
```

Reorder levels on the fly
```{r}
ggplot(____, aes(x=forcats::fct_infreq(____))) + geom_bar() + xlab("cut")
```

## Continuous variable


### Histograms
A histogram of price. 

```{r}
ggplot(dsmall, aes(x=_____)) + _________
```

### Density plots

```{r}
ggplot(______, aes(x=price)) + _______()
```

### Histograms + density 


```{r}
ggplot(_____, aes(x=______)) + geom_density(col="blue") + 
  geom_histogram(___(y=..____..), colour="black", fill=NA)
```

### Boxplots

#### base
```{r}
boxplot(_____$_____)
```

```{r}
boxplot(dsmall$price, horizontal = _____, main="Distribution of diamond prices", xlab="__________")
```


### New variable
```{r}
dsmall$price_range <- cut_width(dsmall$_____/1000, width=5)
```

Create a barchart to visualize the frequencies of each bin. 

```{r}
ggplot(dsmall, aes(x=_______)) + ______
```

----

# Bivariate (Two Variables)

## Categorical v. Categorical

### Two-way Tables

#### Frequency table

```{r}
table(dsmall$___, dsmall$_____)
```

#### Cell proportions

```{r}
_____(dsmall$cut, dsmall$price_range) %>% ________
```


#### Row proportions

```{r}
table(____$____, dsmall$price_range) %>% prop.table(________) %>% _____(3)
```


#### Column proportions

```{r}
table(____$____, ____$____) %>% ________(margin=2) %>% round(_)
```


### Grouped bar charts

```{r}
ggplot(______, ___(_=___, fill=price_range)) + _________() + geom_bar(_______ = "dodge")
```


## Continuous v. Continuous 

### Scatterplot

```{r}
ggplot(dsmall, aes(______, _______)) + geom_point()
```

#### Adding lines to the scatterplots 

```{r}
ggplot(______, ____(_____, y=price)) + _______ + geom_smooth() 
```


```{r}
_____(______, ___(____, ______)) + ________ + 
  geom_smooth(se=FALSE, method="lm", color="blue") + 
  geom_smooth(se=FALSE, color="red")
```


## Continuous v. Categorical


### Overlaid density plots

```{r}
ggplot(______, aes(x=depth, fill=____)) + geom_density()
```


```{r}
ggplot(______, aes(x=____, fill=____)) + geom_density(alpha=__)
```


```{r}
ggplot(dsmall, aes(x=depth, _____=cut)) +__________
```


### Grouped boxplots

```{r}
ggplot(_____, aes(_______, y=carat)) + __________
```

#### Adding violins to the boxplot

```{r}
ggplot(_____, aes(x=_____, y=carat, fill=_____)) +
        geom______(alpha=.1) + 
        geom_______(alpha=.5, width=.2)
```


