 
---
title: "Lab 2: Data Visualization"
author: "Insert Name"
output:
  pdf_document
 
---

```{r, include = FALSE, message = FALSE}
library(tidyverse)
library(nycflights13)
library(NHANES)

```

# Instructions

# Due: beginning of Lab February 11 or 12

# Instructions
Work through the problems below, and submit this document as a knitted .pdf file with answers to the Problem Set 2 assignment on [Gradescope](https://www.gradescope.com/courses/227442/). 

**In some problems, an incomplete R chunk has been provided. To evaluate the chunk, follow the given instructions, and then change `eval=FALSE` to `eval=TRUE`.

**For each problem, put your solution between the bars of red stars.**


# Overview

In this problem set we will use the `ggplot2` package to generate graphics. The *Grammar of Graphics* is the theoretical basis for the `ggplot2` package. Much like how we construct sentences in any language by using a linguistic grammar (nouns, verbs, etc.), the grammar of graphics allows us to specify the components of a statistical graphic.

In short, the grammar tells us that:

> A statistical graphic is a `mapping` of `data` variables to `aes`thetic attributes of `geom`etric objects.

We can break a graphic into the following three **essential** components:

- `data`: the data-set comprised of variables that we plot
- `geom`: this refers to our type of `geom`etric objects we see in our plot (points, lines, bars, etc.)
- `aes`: aesthetic attributes of the geometric object that we can perceive on a graphic. For example, x/y position, color, shape, and size. Each assigned aesthetic attribute can be mapped to a variable in our data-set.

 

### R Packages

R Packages are like apps on your cell phone - they are tools for accomplishing common tasks. R is an open-source programming language, meaning that people can contribute packages that make our lives easier, and we can use them for free.  For this problem set we will use the following R packages:

- `dplyr`: for data wrangling
- `ggplot2`: for data visualization
- `readr`: for reading in data

These packages are already installed for you on Reed's R Studio Server. **Every time** you open a new R session you need to **load (open)** any packages you want to use. We do this with the `library` function.  Run the following in a code chunk to load these packages.

```{r message = FALSE}
library(dplyr)
library(ggplot2)
library(readr)
```

Remember, "running code means" telling R "do this". You tell R to do something by passing it through the console. You can run existing code many ways:

- re-typing code out directly in the console (most laborious method)
- copying and pasting existing code into the console and hitting enter (easier method)
- click on the green triangle in the code chunk (easiest method 1)
- highlight the code and hit `Control-Enter` on PC or `Command-Return` on a Mac (easiest method 2)


### The Data

In this lab we will practice data visualization using data on births from the state of North Carolina. Run the code below to load the data.   

```{r, message = F}
nc <- read_csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vTm2WZwNBoQdZhMgot7urbtu8eG7tzAq-60ZJsQ_nupykCAcW0OXebVpHksPWyR4x8xJTVQ8KAulAFS/pub?gid=202410847&single=true&output=csv")
```

The data set that shows up in your Environment is a large **data frame**. Each observation or **case** is a birth of a single child. 

The `data` command instructs R to load some data built into a package.  The  workspace area in the upper right hand corner of the R Studio window should now list a  data set called `nc` with 800 observations (rows or cases) and 13 variables (columns). 

# How to look at data in R

### Take a glimpse

You can see the **dimensions of this data frame (# of rows and columns)**, the names of the variables, the variable types, and the first few observations using the `glimpse` function. Copy, paste, and run the following in a new code chunk. 

```{r eval = F}
glimpse(nc)
```

We can see that there are 1000 observations and 13 variables in this data set. The variable names are `fage`, `mage`, `mature`, etc. This output also tells us that some variables are numbers (some specifically integers `<int>`, others are numbers with decimals  `<dbl>`).  Some of the variables are factors `<fct>`. It is a good practice to see if R is treating variables as factors `<fct>`; as numbers `<int>` or `<dbl>` (basically numbers with decimals); or as characters (i.e. text) `<chr>`. 

**Exercise 1.** What type of variable is R considering the variable `habit` to be? What variable type is `visits`? (answer with text)

*******************************************************


*******************************************************

### The data viewer

You can view the data by clicking on the name `nc` in the *Environment* pane (upper right window). This will bring up an alternative display of the data set in the *Data Viewer* (upper left window). R has stored these data in a kind of spreadsheet called a *data frame*. Each row represents a  different birth: the first entry or column in each row is simply the row number, the rest are the different variables that were recorded for each birth. You can close the data viewer by clicking on the `x` in the upper left hand corner.


<div id="license">
It is a good idea to try kitting your document from time to time as you go along! Go ahead, and make sure your document is knitting, and that your .pdf file includes Exercise headers, text, and code. Note that knitting automatically saves your Rmd file too!
</div>

# Types of Graphs
We will explore three different types of graphs in this problem set. 

    scatterplots
    boxplots
    histograms

##  Scatterplots
Scatterplots allow you to investigate the relationship between two **quantitative** variables. While you may already be familiar with this type of plot, let's view it through the lens of the Grammar of Graphics. Specifically, we will graphically investigate the relationship between the following two numerical variables in the flights data frame:

- `weeks`: length of a pregnancy on the horizontal $x$ axis and
- `weight`: birth weight of a baby in pounds on the vertical $y$ axis


```{r fig.height=3, fig.width=4, warning=F}
ggplot(data = nc, aes(x = weeks, y = weight)) + 
  geom_point()
```


Let's view this plot through the grammar of graphics. Within the `ggplot()` function call, we specified:

- The data frame to be `nc` by setting `data = nc`
- The `aes`thetic `mapping` by setting `aes(x = weeks, y = weight)`
- The variable `weeks` maps to the `x`-position `aes`thetic
- The variable `weight` maps to the `y`-position `aes`thetic.

We also add a layer to the `ggplot()` function call using the `+` sign. The layer in question specifies the `geom`etric object here as `point`s,  by specifying `geom_point()`.


 
 
**Exercise 2.** Is there a positive or negative relationship between these variables? (text only to answer)


*******************************************************


*******************************************************


**Exercise 3.**  Make a graph showing `weeks` again on the x axis and the variable `gained` on the y axis (the amount of weight a mother gained during pregnancy).   


*******************************************************


*******************************************************

**Exercise 4.**  Study the code below, and the resulting graphical output. Note that a new argument of `color = premie` was added **inside** the `aes`thetic mapping. The variable `premie` indicates whether a birth was early (premie) or went full term. Answer with text:

a) What did adding the argument `color = premie` accomplish? 
     
b) How many **variables** are now displayed on this plot?  
     
c) What appears to (roughly) be the pregnancy length cutoff for classifying a newborn as a "premie"" versus a "full term". 

```{r fig.height=3, fig.width=5, warning=F}
ggplot(data = nc, aes(x = weeks, y = gained, color = premie))+ 
  geom_point() + 
  labs(x = "Pregnancy length (wks)", y = "Maternal weight gain (lbs)")
```


*******************************************************


*******************************************************


**Exercise 5.** Make a new scatterplot that shows a mothers age on the $x$ axis (variable called `mage`) and birth weight of newborns on the $y$ axis (`weight`). Color the points on the plot based on the gender of the resulting baby (variable called `gender`). Does there appear to be any strong relationship between a mother's age and the weight of her newborn? (code and text to answer)
 

*******************************************************


*******************************************************

 
## Histograms 

Histograms are useful plots for showing how many elements of a **single numerical** variable fall in specified bins. This is a very useful way to get a sense of the **distribution** of your data. Histograms are often one of the first steps in exploring data visually. 


For example, to look at the distribution of pregnancy duration (variable called `weeks`), run the following code:

```{r fig.height=3, fig.width=5}
ggplot(data = nc, aes(x = weeks))+ 
  geom_histogram()
```


A few things to note here:

- There is only one variable being mapped in `aes()`: the single numerical variable `weeks`. You don't need to compute the `y`-`aes`thetic: R calculates it automatically.
- We set the geometric object as `geom_histogram()`
- The warning message encourages us to specify the number of bins on the histogram, as R chose 30 for us. 

We can change the binwidth (and thus the number of bins), as well as the colors like so. 

```{r fig.height=3, fig.width=5, warning = F}
ggplot(data = nc, aes(x = weeks))+ 
  geom_histogram(binwidth = 1, color = "white", fill = "steelblue")
```

Note that none of these arguments went inside the `aes`thetic `mapping` argument as they do not specifically represent mappings of variables. 

**Exercise 6.** Inspect the histogram of the `weeks` variable. Answer each of the following with **text**. 

a) The y axis is labeled **count**. What is specifically being counted in this case? Hint: think about what each case is in this data set. 
    
b) What is the approximate average length of pregnancies in weeks?
    
c) If we changed the binwidth to 100, how many bins would there be? Roughly how many cases would be in each bin?
    

*******************************************************


*******************************************************


**Exercise 7.**  Make a histogram of the birth `weight` of newborns.

 
*******************************************************


*******************************************************

### Faceting 
Faceting is used when we'd like to create small multiples of the same plot over a different categorical variable. By default, all of the small multiples will have the same vertical axis.

For example, suppose we were interested in looking at whether pregnancy length varied by the maturity status of a mother (column name `mature`). This is what is meant by "the distribution of one variable over another variable": `weeks` is one variable and `mature` is the other variable. In order to look at histograms of `weeks` for older and more mature mothers, we add a plot layer `facet_wrap(~ mature, ncol = 1)`. The `ncol = 1` argument just tells R to stack the two histograms into one column. 

```{r fig.height=3, fig.width=4, warning = F}
ggplot(data = nc, aes(x = weeks)) +
  geom_histogram(binwidth = 1, color = "white", fill = "steelblue") +
  facet_wrap(~ mature, ncol = 1)
```


**Exercise 8.**  Make a histogram of newborn birth `weight` split by `gender` of the child. Set the binwidth to 0.5. Which gender appears to have a slightly larger average birth weight? (code and text to answer)
 
 

*******************************************************


*******************************************************


## Boxplots

While histograms can help to show the distribution of data, boxplots have much more flexibility, and can provide even more information in a single graph. The y `aes`thetic is the numeric variable you want to include in the boxplot, and the x `aes`thetic is a grouping variable. For instance, below we set `gender` as the `aes`thetic `mapping` for x, and `gained` as the `aes`thetic `mapping` for y. This creates a boxplot of the weight gained for mothers that had male and female newborns.  Note that the `fill` argument is not necessary, but sets a color for the boxplots. 

```{r fig.height=3, fig.width=4, warning= FALSE}
ggplot(data = nc, aes(x = gender, y = gained)) +
  geom_boxplot(fill = "sienna")
```


For review, these are the different parts of the boxplot: '

- The bottom of the "box" portion represents the 25th percentile (1st quartile)
- The horizontal line in the "box" shows the median (50th percentile, 2nd quartile)
- The top of the "box" represents the 75th percentile (3rd quartile)
- The height of each "box", i.e. the value of the 3rd quartile minus the value of the 1st quartile, is called the interquartile range (IQR). It is a measure of spread of the middle 50% of values. Longer boxes indicating more variability.
- The "whiskers" extending out from the bottoms and tops of the boxes represent points less than the 25th percentile and greater than the 75th percentiles respectively. They extend out **no more than** 1.5 x IQR units away from either end of the boxes. The length of these whiskers show how the data outside the middle 50% of values vary. Longer whiskers indicate more variability.
- The dots represent values falling outside the whiskers or outliers. The definition of an outlier is somewhat arbitrary and not absolute. In this case, they are defined by the length of the whiskers, which are no more than 1.5 x IQR units long.




**Exercise 9.** Make a boxplot of the weight `gained` by moms, split by the maturity status of the mothers (`mature`). Is the **median** weight gain during pregnancy larger for younger or older moms? (text and code)

 
*******************************************************


*******************************************************

**Exercise 10** Make a boxplot of pregnancy duration in `weeks` by smoking `habit`. Is the duration of pregnancy more **variable** for smokers or non-smokers? (i.e. which group has the greater spread for the variable `weeks`?). (code and text to answer)

 

*******************************************************


*******************************************************


## Exercise 11

In this problem, we will explore the distribution of finishing times for male and female winners of the New York Marathon between 1970 and 1999. Run the following code to load the `marathon` data set.

```{r}
marathon<-read.csv(file = "/home/courses/math141s20/Data/marathon.csv")
```

a. Create a histogram of marathon times with `binwidth =0.1`, as well as a boxplot of marathon times. What features are apparent in the histogram and not the boxplot? Conversely, what features are apparent in the boxplot but not the histogram?
 
************************************************

************************************************

b. What is one possible explanat
ion for the bimodal distribution?
 
 
 
************************************************


************************************************


c. Construct side-by-side boxplots showing the distribution of marathon times for men and women. Then compare the distribution of times based on the boxplots.

************************************************


************************************************

d. Create a scatterplot with year along the $x$-axis and marathon time along the $y$-axis. Use `shape` to distinguish between male and female runners. Describe what is visible in this plot but not in the others.
 

************************************************


************************************************


## Exercise 12

In this problem, we will use the `NHANES` dataset which can be found in the `NHANES` package.  Make sure to run the following chunk to load the dataset.

```{r}
# Load NHANES dataset
data(NHANES)
```

a. Construct a scatterplot with `Age` on the x-axis and `Height` on the y-axis.

*************************************************


*************************************************

 
b. Recreate the scatterplot but this time modify the plot to combat the overfitting issue.

*************************************************


*************************************************
 
c. The geom, `geom_smooth()`, will add a smooth curve that tries to capture the general trend.  Add this layer to scatterplot.

*************************************************


*************************************************

d. Describe and explain the shape.  

*************************************************


*************************************************


e. Based on the graph, what is the maximum age in the dataset?  Why would the researchers round down the ages of the oldest subjects?

*************************************************


*************************************************


## Exercise 13

In this problem, we use data from the `pdxTrees` package.  In particular, we will use the dataset called `near_reed` that I create below, **not** the full dataset, `pdxTrees_parks`.  This dataset includes the trees from four parks that are close to Reed.  


Make sure to run the following R chunk.  

```{r}
# Grab trees near Reed
library(pdxTrees)
pdxTrees_parks <- get_pdxTrees_parks()

near_reed <- filter(pdxTrees_parks, Park %in% c("Crystal Springs Rhododendron Garden",
                                          "Kenilworth Park", 
                                          "Eastmoreland Garden",
                                          "Berkeley Park"))

#Remove trees with no functional type
near_reed <- drop_na(near_reed, Functional_Type)
```




a. Create a bar plot of `Park` and add the following layer to flip the axes: `coord_flip()`.


*************************************************


*************************************************

b. Now,reorder the bars to make it easier to compare the number of trees in the parks.  The function `fct_infreq()` printed below will reorder the categories by their frequencies.  After reordering `park`, recreate the bar plot and draw some conclusions from your graph.



*************************************************

```{r}
# Change the order of park
near_reed <- mutate(near_reed, 
                    Park = fct_infreq(Park))
```

*************************************************

c. Create a segmented bar plot of `Park` and `Functional_Type` where the plot displays the proportions of each of the functional types for each park.


*************************************************


*************************************************
 
d. Draw some conclusions from your graph in c. (Use `?get_pdxTrees_parks` to see what the `Functional_Type` categories represent.)

*************************************************


*************************************************

e.  For the segmented bar plot, you can change the color of the bars by adding the following layer to a ggplot: `scale_fill_brewer(type = "div", palette = 3)`. Make several graphs of `Park` and `Mature_Size` and try different palette types ("div", "seq", "qual") and colors (typically 1-9).  **Once you've explored several options, only include the graph that best displays the information.  Give the graph nice axis labels and a title.**


*************************************************


*************************************************


f. Justify the color palette you selected in e.

*************************************************

 
*************************************************


