---
title: "Matching Visualization to Data"
subtitle: "Sections: 1.4-1.5: Tuesday, Week 2"
format: html
toc: TRUE
toc-title: Outline
---

# Getting Started

Some reminders & preliminaries

## Tidbits to be covered

1. Dealing with x-axis labels that are too crowded. (`theme()` and `element_text()` or `guides()`)

## Load libraries
```{r warning=FALSE, message=FALSE}
  library(tidyverse)
```
  
#### Save your work

The following two ways of saving work should be used:

- Scrpts: In Positron, New File > R script.  You can save R-commands and include documentation in a script.  This is useful for class work and/or homework.  You can also share scripts with others.  Make sure that scripts *work* before you move on to other things. 

- Quarto Documents: Similar to a script, but you can include text and graphics in a Quarto Document.  You can share Quarto Documents or convert them (render) to other formats such as HTML & PDF. 

#### We'll use `storms` and `gss_cat` both included with the Tidyverse 

Either view the data in the editor window or `glimpse` it in the console.
```{r}
data()
```

## Here are 6 Ways to Visualize Data

#### Categorical Distributions

If you want to see how many samples there are in each category you can use a bar plot. 

Create a bar plot with geom_bar(). **Compare two categorical variables** - one category is represented bar a bar and we fill each bar with another variable.

Use dataset `gss_cat`, A sample of categorical variables from the General Social survey.

```{r}
glimpse(gss_cat)

ggplot(gss_cat, aes(x = race)) +
  geom_bar(aes(fill = marital))
```

#### Numerical Distrubutions

If you want to see **how many things there are having roughly the same numerical value** using a histogram is useful.  You can set `binwidth` to different values to reflect what *roughly* means.  This would be good for test scores, etc. 


Leave + signs at **the end** of the line, use indentation and alignment.  Break you code into chunks, - start with the main 3 ingredients and build up slowly.

```{r}
#| label: time_hist

ggplot(storms, aes(x = lat)) + 
  geom_histogram(binwidth =  .5, aes(fill = status)) +
  labs(x = "Latitude", y = "Number of storms", 
       title = "Latitude Distribution", 
       subtitle = "Half degree intervals")
```

## Relationships b/w a Numerical & Categorical Variable

In the storms data how do the latitudes (a numerical value) vary among the status categories?  

#### Box plot

```{r}
ggplot(storms, aes(x = status, y = lat)) +   
  geom_boxplot() + 
  theme(axis.text.x = element_text(angle = 60, hjust = 1 ))
```

Another way to deal with crowded x-axis labels

```{r}
ggplot(storms, aes(x = status, y = lat)) +   
  geom_boxplot() + 
  scale_x_discrete(guide = guide_axis(n.dodge = 2))
```
  
To explore these box plots, use filter to look at some actual data entries. 
How do the numbers in the data reflect what you see in the boxplot above?

**Exercise** Make the same histogram above, but with a subset of the data.  


Vary `binwidth` and use  `scale_x_continuous` to change the labels on the x-axis.  Also, notice that we filled each bar with a `fill = age` aesthetic and we got capital `Age` in the legend in the `labs()` function.  

```{r}

hi_storms <- filter(storms, lat > median(lat))
ggplot(hi_storms, aes(x = lat)) + 
  geom_histogram(binwidth =  .5, aes(fill = status)) +
  scale_x_continuous(breaks = seq(7,70,by = 0.5)) + 
  labs(x = "Latitude", y = "Number of storms", 
       title = "Latitude of Storms", 
       fill = "Storm Type") +
  theme(axis.text.x = element_text(angle = 60, hjust = 1 ))

```

**Exercise** Improve the previous plot by changing the scale on the x-axis to improve readability.  


## Smooth Histograms

These are useful with large amounts of numerical data.  

Use a density plot display a smooth histogram,

```{r}
ggplot(storms, aes(x = lat, 
  color = status, fill = status)) +
  geom_density(alpha = 0.5)
```

Another view of the data through a box plot.  What conclusions can you draw about the differences between the kinds of storms from the data below?

```{r}
ggplot(storms, aes(x = status, y = lat)) +   
  geom_boxplot()
```


## Three or more numerical values

It's possible, but be careful not to over-do it.  Too much information on a graphic is not good.

```{r}
youth <- filter(gss_cat, age < 30)
ggplot(youth,
       aes(x = age, y = rincome)) +
       geom_point(aes(color = denom, shape = marital))
```

## Assignment 3: 

- Due in the Samba folder - Midnight, Monday 9/14
- Answer all exercises in 
  - <https://r4ds.hadley.nz/data-visualize.html#exercises-2>
  - <https://r4ds.hadley.nz/data-visualize.html#exercises-3>


