---
title: "Position Adjustments"
subtitle: ""
toc-title: "Chapter 9: Section 9.6"
theme: flatly 
date: today
toc: true
format:
  html: 
    code-overflow: wrap
    toc: true
    toc-title: "Contents"
    # hide code?

execute:
  warning: false
  message: false
---


## Warm-up: Data in the News

* Follow [this link](https://www.npr.org/sections/health-shots/2023/03/25/1164819944/live-free-and-die-the-sad-state-of-u-s-life-expectancy).
* Exercise: re-create the plot.
* Click on the **Source** link in the caption, then scroll down to the caption and follow the **Get the data** link.

## NMU Honors data
```{r}
library(tidyverse)
# h1 is honors students by department
# h2 is all  students by department
nmu <- read_csv("https://euclid.nmu.edu/~joshthom/Teaching/DAT309/Week7/Honors/h1.csv")
```

## Position Arguments: Stacking

Know what each of these do with a barplot: `geom_bar(position = "...")`
  i) "identity"
  ii) "dodge"
  iii) "fill"
  iv) "stack
  
Create something like this, but use Honors / No Honors instead of Class Level.

![stacking](stacking.png)

Read the following tips from the [reference page on stacking](https://ggplot2.tidyverse.org/reference/position_stack.html)
 
## Variation

Tendency of variable to change *within* a dataset. Display the variation of some variables within the `diamonds` included with R. The displays below use `cowplot`, see this [vignette](https://wilkelab.org/cowplot/articles/plot_grid.html) for more details.

```{r}
#| eval: false
install.packages("cowplot")
```

```{r}
# variation of the price of diamonds in this data
library(cowplot)
library(tidyverse)
# count the number of rows (diamonds) in the data
diamonds |> count()

prs <- ggplot(diamonds, aes(x = price)) +
  geom_histogram(binwidth = 100)

crt <- ggplot(diamonds, aes(x = carat)) +
  geom_histogram(binwidth = .25)

plot_grid(prs,crt,labels = "AUTO")
```

### Typical Values

In both histograms and bar charts - tall bars means lots of data, thus values that are very common. Short bars less common. No bars mean no data.

1.  *Which values are the most common? Why?*

2.  *Which values are rare? Why? Does that match your expectations?*

3.  *Can you see any unusual patterns? What might explain them?*

[Visualizations can reveal *clusters*.]{.underline}

1.  *Clusters for one variable: concentration in the distribution*

2.  *Clusters of multiple variables: high co-variation*

#### Questions concerning clusters

1.  *How are the observations within each subgroup similar to each other?*

2.  *How are the observations in separate clusters different from each other?*

3.  *How can you explain or describe the clusters?*

4.  *Why might the appearance of clusters be misleading?*

#### Unusual values

During our EDA, we encounter the plot below. The large blank area in the x-axis seems suspicious. There are several ways to explore this suspicion.

1.  Zoom-in to see more via `coord_cartesian` or `ylim` and `xlim`.
2.  Create a small dataset that shows more detail about the unusual values.

[Explore both of these strategies now.]{.underline}

```{r}
# 
ggplot(diamonds, aes(x = y)) + 
  geom_histogram(binwidth = 0.5)
```

What do we do about the anomalous values?

1.  Remove the entire row:

```{r}
diamonds2 <- diamonds |> 
  filter(between(y, 3, 20))
```

2.  Replace the unusual values with NAs (missing values).\

```{r}
diamonds2 <- diamonds |> 
  mutate(y = ifelse(y < 3 | y > 20, NA, y))
```

Recall, `ggplot()` does not plot NA's but does warn you about them, unless you do this:

```{r}
ggplot(diamonds2, aes(x = x, y = y)) + 
  geom_point(na.rm = TRUE)
```

## Co-variation

Co-variation is the tendency of multiple variables to vary in a related way, it is evidence of relationships *between* variables.

#### Relationship b/w categorical and numerical variable

```{r}
# histogram of price, colored by cut
ggplot(diamonds, aes(x = price)) + 
  geom_freqpoly(aes(color = cut), binwidth = 500, linewidth = 0.75)
```

Note the nice (ordered) coloring - it's due to the fact that `cut` is an ordered factor.

```{r}
glimpse(diamonds)
```

To determine co-variation we need to see the same pattern in multiple variables. But the differences in scale obscure this. The fix:

```{r}
ggplot(diamonds, aes(x = price, y = after_stat(density))) + 
  geom_freqpoly(aes(color = cut), binwidth = 500, linewidth = 0.75)
```

What are common traits of expensive diamonds? What about less expensive diamonds?

```{r}
# expensive diamond distribution of color and cut
expensive <- diamonds |> filter(price > 2000)
expensive |> ggplot(aes(x=cut, y= color)) + geom_count()
```




