---
title: "Homework 3"
author: "NAME"
date: "DATE"
output:
  html_document: default
  pdf_document: default
---

# Introduction

You will be creating some basic plots using `ggplot2` package and managing data using `dplyr` package in this homework assignment. You will use two data sets, `ncbirths` which is in `openintro` package and `flights` which come part of the `nycflights13` package. 

The code chunk below sets some code chunk options (using `opts_chunk` from the `knitr` package) to make your knitted report output more readable. It is good habit to load all packages and data in the first code chunk. 

```{r, warning=FALSE, message=FALSE}
knitr::opts_chunk$set(warning=FALSE, message=FALSE, fig.height=4, fig.width=5, fig.align='center') 
library(ggplot2)
library(dplyr)
flights <- nycflights13::flights
ncbirths <- openintro::ncbirths
```

# Univariate plots

This section asks you to create data visualizations or summaries from the `ncbirths` data set. 

1. Create a table of marital status (`marital`) from NCbirths. 

```{r}

```

2. Create a barchart of marital status (same as above)

```{r}

```

3. Create a histogram of mother's age (`mage`) with an overlaid density plot in a different color. Be sure that both density curve and histogram can be seen. 

```{r}

```

4. Create a horizontal boxplot of weight of the baby (`weight`)
I don't care how you make this plot, so long as it looks like a horizontal boxplot.

```{r}

```

# Bivariate plots (This section still uses the `ncbirth` data set.)

1. Create a two-way frequency table of maturity status (`mature`) against smoking `habit`

```{r}

```

2. Create a proportion table of smoking habit _within_ maturity status. Round to 3 digits. 

```{r}

```

3. Create a grouped barchart that reflects the frequencies you calculated above. Think carefully which variable goes on the `x` axis, and which one is used for the `fill`.

```{r}

```

4. Create a scatterplot of length of pregnancy in `weeks` and the babies `weight`. Include a smoother line in brown, and a best fit linear model line in purple.
    
```{r}

```
    
5. Create a grouped boxplot of baby `weight` by mothers smoking `habit`. Make sure you `fill` the boxes by `habit` as well. 

```{r}

```

6. Replicate the same plot as above, but overlay a violin plot and change the transparency of both violin and boxplot layers. 

```{r}

```

7. Create an overlaid density plots of weight `gained` by babies `gender`. Do not apply a `fill`, only use the `color` aesthetic. 

```{r}

```

# Data management and aggregation

This section uses the `dplyr` and `nycflights13` packages. Use the `flights` data set for the next few exercises. 

* At each step use the assignment operator `<-` to store the results into a new data table and use that data in the next step. 
* At each step, print out the resulting data frame so you can see the results. 

Example _(not run)_ 
```r
p1 <- planes %>% select(type)
p1
```

1. Use `select()` to extract the following variables: `origin`, `distance`, and `air_time`, `dest`. Save this result as a data set named `f1`. 
```{r}

```

2. Take the `f1` data set and `filter()` to select only the flights whose destination (`dest`) is Atlanta (ATL). Save this result as `f2`. _Hint: the destination variable is a character variable, so think carefully about how you specify ATL_. 

```{r}

```

3. Take the `f2` data set and use `mutate()` to create a new variable `speed` that calculates speed of the plane as `distance/air_time*60`. Save this result as `f3`. 
```{r}

```

4. Use `ggplot` to plot the distribution of the planes speed on it's way to Atlanta using a histogram. 
```{r}

```

5. Use `dplyr` chaining magic (%>%) to combine questions 1-4 in one step. 

```{r}

```

6. The three airports in the NYC region are all pretty close together. Do they all have the same travel time to Chicago O'Hare (ORD)? Use the same tactic as you did in steps 1-4 (or 5) by subsetting to the desired destination, then create overlapping density plots of `air_time` where each density plot is colored (or filled) by the variable `origin`.

```{r}

```

# Challenge questions (1 pt extra credit)

1. Repeat the process in problem 6, but when creating overlapping density plots, instead of `air_time`, define and use `speed` as in problem 3.

```{r}

```