---
title: 'Homework 4: Data Import and Enhanced Graphics'
author: "NAME"
date: "DATE"
output: html_document
---

```{r}
knitr::opts_chunk$set(fig.width=6, fig.height=4) # This sets all figure sizes in the document unless otherwise specified.
library(dplyr)
library(ggplot2)
```

# Importing data
Go to [Dr. D's data website](https://www.norcalbiostat.com/data/) to find the following data files. Download them to your computer (ref: [HW 09](../09_import.html)) and import them in the first three questions below.  _NOTE: Be sure you download the CSV file as .csv and not as a .numbers file. If you create a file in Numbers, you can choose File > Export To > CSV to convert it._

1. Read in the `High School and Beyond` data set. Use R functions to create a table that shows how many students are in each program (using the `prog` variable).



2. Read in the `Crime Data` data set. Use R functions to create a table that shows how many states are in each division (using the `division` variable).


3. Read in the `countyComplete` data set.


4. When reading in the Excel data set on crime, what do the arguments `sheet=1` and `col_names=TRUE` mean? 


5. How many observations and variables are contained in the `countyComplete` data set? Explain how you found this information. 


----


# Graphing Enhancements
This section will ask you to create graphics, often on data that needs pre-processing using the dplyr verbs you learned in the last homework. No one expects you to have all those commands memorized yet, the key is to know how to go find what you need. So reference last week's notes and homework while you do this section. 

## Exploring the poverty rate 
This section demonstrates how you can build a nice graph in stages. Each question will have you adjust one thing on the plot. Instead of retyping the entire code each time, copy the code from the prior question and then make the requested adjustment.

NOTE: I would highly suggest word-wrapping your code for these questions. Go to Tools > Global Options > Code and check "Soft-wrap R source files". Be sure you click "Apply" once you check the box.

> This is the suggested way of building an advanced plot. Small bit by bit, changing or adding only one thing at a time. 

1. Using the `crime` data, create a density plot of the distribution of `poverty` rate (x) by `region` (fill). To avoid problems with overplotting, use `alpha=.5` inside the density geometry to make the colors transparent. 


2. These are pretty diverse regions, let's see how the poverty rate varies within region. Fill by `division` instead, and panel on `region`. 


3. Add a different theme to this plot. _Hint: Do not use theme_void() for this step_


4. Change the colors to use the `Set1` palette. 


5. Capitalize the legend title. _Hint, you're adding something to the_ `scale_` _section. Be sure that you continue to build off your code from the previous steps so that your theme is still present_. 

6. This works, but I'm not entirely thrilled with how much space it's taking up. Let's look at these as boxplots instead of density plots. Start by setting up vertical boxplots, putting your continuous variable on the y axis. 

7. The x-axis is using numbers instead of names now. Let's fix that by clearly defining that  x is division. _Note, The division levels will appear to blend together, but we'll be fixing that in later steps._

8. In this case, Division is nested within Region and so we don't need ALL the levels of division to show up in each panel. Let's "Free" our x axis by adding `scales="free_x"` to the `facet_wrap`. 


9. But I really want to see them as horizontal plots, so let's add a `+ coord_flip()` layer. This also helps with readability of long axis names. _Note, you will also need to change your scales to free the y-axis instead now._

10. The legend is kinda useless now because we have the labels clearly written. Add `guide='none'` to the `scale` layer. 

11. Add `ncol=1` to the `facet_wrap` to stack the panels vertically. 



12. Add an appropriate title and axis titles.  _NOTE: Be sure your axes are correct, due to the effects of coord_flip()._



13. One last touch: adjust the figure size by selecting a new size for your code chunk using "fig.height=" and "fig.width=" in your code chunk options.

# Challenge Questions (1 pt extra credit - 0.5 per question)

1. Using the `High School and Beyond` data set you loaded earlier, construct a boxplot for the Writing Scores `write` by Program `prog` that fills by School Type `schtyp`. To receive full credit, be sure you include a title for your plot, label your axes, and give your legend the title "School Type".



2. Using the `countyComplete` data set you loaded earlier, construct a scatterplot for `per_capita_income` against Percentage of Bachelors Degrees `bachelors`, for ONLY the data from California, Oregon, and Washington. Color and panel on `state`, and remove the legend. As above, include a title for your plot and label your axes.