---
title: 'Data Viz with ggplot'
author: 'DSC 365: Introduction to Data Science'
date: "January 27, 2026"
format: html

execute: 
  fig-width: 5
  fig-height: 2

---

::: {.callout-note icon="false"}
## Recommended Reading:
- _Modern Data Science with R_ Ch. 2: Data Visualization
- _Modern Data Science with R_ Ch. 3: A Grammar for Graphics

:::

## `ggplot2`

In the lecture, we showed that statistics alone may lead to a misunderstanding of the data. Therefore, when working with new data, we should always make some visualizations to help us understand the data. A common way for plotting in R today is through `ggplot2`. 

`ggplot2` is an R package (located in `tidyverse`) for "decoratively creating graphics"

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

## Example: Hate crimes and income inequality

A FiveThirtyEight article published in 2017 claimed that higher rates of hate crimes were tied to greater income inequality. (https://fivethirtyeight.com/features/higher-rates-of-hate-crimes-are-tied-to-income-inequality/)

- FiveThirtyEight publishes their data sets (and even has an R Package) - let's investigate for ourselves.

Data set is posted in BlueLine. Download this data set, and save it to your computer. Follow these steps to read the data into RStudio:

1. In the Environment tab, click “Import Dataset”. Since this is a CSV document, you want to import a text file.
2. Navigate to your CSV data set. Make sure that the first row contains column names.
3. Import the data.

```{r}
#Update what is inside "" with the file path on your computer

hate_crimes <- read_csv("data/hate_crimes.csv")
```


## Variable types

After specifying the data and aesthetics, we need to decide the plot type. In order to do that, we need to know the variable type(s). There are two different ways to distinguish the variables. 

By function: 

1. response variable
2. explanatory variable

By value type:

1. numerical variable
2. categorical variable

## Type of plots

#### 1. Visualize one numerical variable.

Usually for the response variable using histograms and density plots


#### (a) Histograms

```{r}

```

#### (b) Density Plots

```{r}

```

#### (c) Box Plots

Visual representation of the 5-number summary.

```{r}

```

#### 2. Visualize one categorical variable

```{r}

```

**Note**: use `geom_col` if want the heights of the bars to represent values in the data.

#### 3. Visualize two numerical variables. 

Focusing on relationship between them. Can be response variable + explanatory variable. Can also be explanatory variable + explanatory variable.

#### (a) Scatterplots

```{r, fig.align='center'}

```

#### (b) Line plots and Smooth Line Plots to connect the points in the scatterplot. 

The Smooth Line Plots help show the trend due to smoothness

```{r}

```


## 5. Visualize one numerical variable and one categorical variable (Multiple groups)

Sometimes we want to compare the variable(s) across multiple groups. eg: compare median income across different region. 

Theses are called side-by-side plots. 

```{r, fig.align='center'}

```

Wait, how can I know which group is which group?

#### Include options like color and size

```{r, fig.align='center'}

```

#### Adding plot title and changing axis titles

Add x-axis, y-axis labels and title

```{r}

```

#### Faceting by groups

Instead of putting all groups information into one page, you can do by each panel.

```{r}

```


#### Try it for yourself

1. Suppose we are interested in the unemployment rate and want to see its distribution. 

```{r}

```

2. Suppose we want to show the relation between unemployment rate and median income. 

```{r}

```

3. Report an approximate median for the unemployment rate. 

```{r}

```

4. Show the unemployment rate across different region. Use color to indicate different regions.

```{r}

```

5. Show the relation between unemployment rate and FBI hate crime rate. Use size to indicate gini index. Make sure to include axis labels and title. 

```{r}

```

6. Plot the distribution of gini index and put differnet region on different panel. 

```{r}

```

