Day 12 - Advanced Plotting

Fall 2022

Dr. Jared Joseph

October 05, 2022

Overview

Timeline

  • Why Visualize?
  • Components of Visualization
  • Ways to Visualize
  • Good and Bad Data Visualization
  • ggplot2 and the Grammar of Graphics

Goal

Learn how to make plots that don’t hurt to look at.

Why Visualize?

We’re Wired that Way

Gestalt Principles for Attention and Segmentation in Natural and Artificial Vision Systems

To Understand our Data

Datasaurus - Alberto Cairo

To Tell a Story

Sophia’s Story

Data Visualization ≠ Infographic

Data Visualization

Is a visualization of data, often used to tell as story.

Infographic

Tells a story with a visual aid.

Components of Visualization

Our Toolbox

  • Position
  • Length
  • Angle
  • Slope
  • Area
  • Volume
  • Density
  • Color

Ways to Visualize

Bar Chart

```{r}
#| fig-cap: Barplot of Mint x Chocolate Ratings

barplot(table(survey$mint_choc))
```

Barplot of Mint x Chocolate Ratings

Histogram

```{r}
#| fig-cap: Histogram of typical hours sleep

hist(survey$hours_sleep)
```

Histogram of typical hours sleep

Scatterplot

```{r}
#| fig-cap: Scatterplot of Weight x Miles per Gallon

plot(mtcars$wt, mtcars$mpg)
```

Scatterplot of Weight x Miles per Gallon

Line Chart

Boxplot

```{r}
summary(mtcars$mpg)
```
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  10.40   15.43   19.20   20.09   22.80   33.90 
```{r}
#| fig-cap: Boxplot of Miles per gallon

boxplot(mtcars$mpg)
```

Boxplot of Miles per gallon

Good and Bad Data Visualizations

Good - 1

Charles Joseph Minard

Good - 2

Reddit - u/citrusvanilla

Good - 3

Reddit - u/tigeer

Bad - 1

Bad - 2

Bad - 3

ggplot2 and the Grammar of Graphics

ggplot works in layers

Building ggplot layers

```{r}
#| output-location: column

library(ggplot2)

ggplot(survey) 
```

Building ggplot layers

```{r}
#| output-location: column

library(ggplot2)

ggplot(survey) +
  aes(x = nerd)
```

Building ggplot layers

```{r}
#| output-location: column

library(ggplot2)

ggplot(survey) +
  aes(x = nerd) +
  geom_bar()
```

Building ggplot layers

```{r}
#| output-location: column

library(ggplot2)

ggplot(survey) +
  aes(x = nerd) +
  geom_bar() +
  scale_x_discrete(labels = c("No", "Yes"))
```

Building ggplot layers

```{r}
#| output-location: column

library(ggplot2)

ggplot(survey) +
  aes(x = nerd) +
  geom_bar() +
  scale_x_discrete(labels = c("No", "Yes")) +
  labs(x = "Does the respondent identify with the label 'nerd'?",
       y = "People",
       title = "Previlance of nerds in SDS 192-03",
       subtitle = "SDS 192-03 Fall 2022")
```

Building ggplot layers

```{r}
#| output-location: column

library(ggplot2)

ggplot(survey) +
  aes(x = nerd) +
  geom_bar(fill = c("dodgerblue2", "forestgreen")) +
  scale_x_discrete(labels = c("No", "Yes")) +
  labs(x = "Does the respondent identify with the label 'nerd'?",
       y = "People",
       title = "Previlance of nerds in SDS 192-03",
       subtitle = "SDS 192-03 Fall 2022")
```

Building ggplot layers

```{r}
#| output-location: column

library(ggplot2)

ggplot(survey) +
  aes(x = nerd) +
  geom_bar(fill = c("dodgerblue2", "forestgreen")) +
  scale_x_discrete(labels = c("No", "Yes")) +
  labs(x = "Does the respondent identify with the label 'nerd'?",
       y = "People",
       title = "Previlance of nerds in SDS 192-03",
       subtitle = "SDS 192-03 Fall 2022") +
  theme_minimal()
```

That’s a Lot of Code…

```{r eval=FALSE}
library(ggplot2)

ggplot(survey) +
  aes(x = nerd) +
  geom_bar(fill = c("dodgerblue2", "forestgreen")) +
  scale_x_discrete(labels = c("No", "Yes")) +
  labs(x = "Does the respondent identify with the label 'nerd'?",
       y = "People",
       title = "Previlance of nerds in SDS 192-03",
       subtitle = "SDS 192-03 Fall 2022") +
  theme_minimal()
```

There are Tools to Help

Mosaic

Esquisse

Just know these tools will only get you so far!

For Next Time