---
title: " and the Critique"
author: J. Hathaway
params:
  day: 6
  ptitle: true
  pbackground: true
  dtype: "none"
---


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)

if (params$day %% 2 == 0) md_intro_format <- "even_intro_format.Rmd"
if (params$day %% 2 == 1) md_intro_format <- "odd_intro_format.Rmd"

```

# Becoming the Critic. {data-background=#e8d725}

```{r nextone}
vday <- params$day - 1
vlink <- paste0("http://www.perceptualedge.com/example", vday, ".php")
titlelink <- paste0('{data-background-iframe="', vlink, '"}')
```

## `r titlelink`

[Visualization of the Day](`r vlink`)


```{r child=md_intro_format}

```



# Providing Critiques and the Routine {data-background=#e8d725}

## A few critique examples

* [Mckay's](https://github.com/BYUI335/Davis_M/issues/5):  Notice how he responds that he has addressed the issues.
* [Aletha's Request](https://github.com/BYUI335/Pyeatt_A/issues/5):  Notice her request and the responses.
* [Hathaway's Example Response](https://github.com/BYUI335/Pyeatt_A/issues/7): Notice the formatting and references.


# The Graphic Tool Set {data-background=#e8d725}

## The grammar of graphics

> - [An overview of the grammar](https://www.youtube.com/embed/uiTc55clwuA?rel=0)
> - [ggplot2 specifics](https://www.youtube.com/embed/PiY9hwOkL8U?rel=0)

## Cleveland's Examples

There were three democratic candidates.  We are going to use the following plots to help us understand the constituency that helped the winner in the primaries.

- [1984 Primaries](https://en.wikipedia.org/wiki/Democratic_Party_presidential_primaries,_1984)
- [Mondale, Hart, Jackson Visualization](../images/Cleveland_1_ok.png){target="blank"}
- [Better Mondale, Hart, Jackson Visualisation](../images/Cleveland_1_better.png){target="blank"}
- How much time do you think it took Bill Cleveland in th 80's to make each graph?

## Class Activity: Getting Started

```{r gettingstarted, eval=FALSE, echo=TRUE}

#install.packages("nycflights13")
library(nycflights13)

fl_bp <- flights %>%
  ggplot(aes(x = carrier, y = dep_delay))
fl_sc <- flights %>%
  filter(dep_time > 800, dep_time < 900) %>%
  ggplot(aes(x = dep_time, y = dep_delay))

fl_bp + geom_boxplot()
fl_sc + geom_point()

```

Get the above code working.  We will be tweaking it using ggplot for the next part.

## Labelling the outside

Complete the following - **Create clean labels for the x and y axes and zoom in on the y-axis from 50 to 100 minutes. Also have breaks every 15 minutes**
   
> - labelling - Axes, Titles, and Legends
> - xy Scales - `scale_x_`, `scale_y`, `coord_` - Setting breaks and changing labels *or* Transforming scale

```{r labelzoom, eval=FALSE, echo=FALSE}
# be careful with limits in scale_
fl_bp + geom_boxplot() + scale_y_continuous(limits = c(-50, 100))
fl_bp + geom_boxplot() + coord_cartesian(ylim = c(-50, 100))

```


## Labelling the inside

Complete the following - 

> 1. Color the points of `fl_sc` by `origin` using the brewer scale. 
> 2. Color the points of `fl_sc` by `arr_delay`**

> - color & fill scales   
>     - `scale_color_`
>     - `scale_fill_`
>     - `scale_gradient_`

```{r colorinside, echo=FALSE, eval=FALSE}
fl_sc + geom_point(aes(color = origin), size = 3) +
  scale_color_brewer(type = "qual") + theme_bw()

fl_sc + geom_point(aes(color = arr_delay), size = 3) +
  scale_color_continuous()  + theme_bw()

```


## Putting the legend on the inside

Complete the following - **1) Color the points of `fl_sc` by `origin` using the brewer scale and use the directlabel package to move the labels into the plotting region.**

> * `library(directlabels)`
>    * http://directlabels.r-forge.r-project.org/examples.html
>    * `geom_dl()` and `direct.label()`

```{r dl1, eval=FALSE, echo=FALSE}
library(directlabels)
bob <- fl_sc + geom_point(aes(color = origin), size = 3) +
  scale_color_brewer(type = "qual") + theme_bw()
direct.label(bob)

```


## Changing the feel

Complete the following - **Use a `theme_()` to create a different look for your graphic and change the orientiation of the x-axis test to 35 degrees**

> * Themes (check out `library(ggthemes)`)
> * `ggsave()`

```{r rotateaxis, eval=FALSE, echo=FALSE}
library(ggthemes)

fl_bp + 
  geom_boxplot() + 
  coord_cartesian(ylim = c(-50, 100)) +
  ggthemes::theme_fivethirtyeight() +
    theme(axis.text.x = element_text(angle = 35))

```


## Scales

Each of the aesthetics has a paired scale function - x, y, size, color, fill, linetype, shape, alpha.  All of the scales start with `scale_` and then the respective aesthetic. All the aesthetic scales have an `_continuous`, `_discrete`, and `_manual`.  

> - `scale_x_` & `scale_y_` are the two scales I most often use
> - `scale_fill_` & `scale_color_` are the next most often used.
>     - manual()
>     - gradientn()

## Labelling Elements inside the graphic

The `library(ggrepel)` package is a must for our work. `library(directlabels)` can also be helpful. 
Here is the [book's graphic](http://r4ds.had.co.nz/communicate-plots_files/figure-html/unnamed-chunk-9-1.png). 

> - What are some concerns you have with this graphic?

## Code and graphic (scales)

Here is the [book's graphic](http://r4ds.had.co.nz/communicate-plots_files/figure-html/unnamed-chunk-9-1.png).  
Use the [code from 28.3](http://r4ds.had.co.nz/graphics-for-communication.html) and update their graphic to match mine.


```{r nnn2, fig.width=10, fig.height=4, echo=FALSE}
library(ggrepel)
library(viridis)

best_in_class <- mpg %>% 
  group_by(class) %>% 
  filter(row_number(desc(hwy)) == 1)

ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(colour = class), size = 3) +
  geom_point(size = 1.5, data = best_in_class, color = "white") +
  geom_text_repel(aes(label = model, colour = class), 
                  data = best_in_class, show.legend = FALSE, 
                  nudge_x = -1, nudge_y = -2) +
  theme_bw() + 
  theme(panel.grid.minor = element_blank()) +
  scale_color_viridis(discrete = TRUE) + 
  labs(x = "Engine displacement", y = "Miles per gallon (highway)", 
       color = "Vehicle type")
```

## My Code Solution

```{r newname_code, eval=FALSE, echo=TRUE}
library(ggrepel)
library(viridis)

best_in_class <- mpg %>% 
  group_by(class) %>% 
  filter(row_number(desc(hwy)) == 1)

ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(colour = class), size = 3) +
  geom_point(size = 1.5, data = best_in_class, color = "white") +
  geom_text_repel(aes(label = model, colour = class), 
                  data = best_in_class, show.legend = FALSE, 
                  nudge_x = -1, nudge_y = -2) +
  theme_bw() + theme(panel.grid.minor = element_blank()) +
  scale_color_viridis(discrete = TRUE) + 
  labs(x = "Engine displacement", y = "Miles per gallon (highway)", 
       color = "Vehicle type")
```




# Displaying multiple distributions {data-background=#e8d725}

## Clarity vs. Complication

Data can get complicated very fast. How do we provide depth of variability understanding without overwhelming the visualization user?

> * [violin plots (`geom_violin()`)](http://eamoncaddigan.net/dataviz/r/psych/2015/09/26/violin-plots/)
> * [beeswarm plots (`ggbeeswarm::geom_quasirandom()`)](https://github.com/eclarke/ggbeeswarm)
> * [letter-value boxplots `lvplot::geom_lv()`](https://github.com/hadley/lvplot). [Here](https://mgimond.github.io/ES218/Week08b.html) is a description.

Another package that makes flipping the axes easier in ggplot -- [rotating axes (`ggstance`)](https://github.com/lionel-/ggstance)

> * [link to last presentation](day_5.html#/displaying-distributions-of-multiple-groups-for-decision-making)

## Clarity vs. Complication (2)

> - What do we know after looking at this plot?
> **How do we provide depth of variability understanding without overwhelming the visualization user?**


```{r cc1, echo=FALSE, fig.height= 3}
library(nycflights13)
library(tidyverse)
library(magrittr)
library(forcats)

flights %>% 
  group_by(carrier) %>% 
  summarise(dep_delay = mean(dep_delay, na.rm = TRUE)) %>%
  ggplot(aes(x = fct_reorder(carrier, dep_delay), y = dep_delay)) +
  geom_col() +
  labs(x = "Airline Carrier Short Name", y = "Average Departure Delay", title = "Typical Delay of Flights by Airport") + theme_bw()

```



Remember, data can get complicated very fast. 


```{r, echo=FALSE, results=FALSE, message=FALSE, warning=FALSE}
pacman::p_load(ggbeeswarm, lvplot)

```


## Distribution background

* [violin plots](http://eamoncaddigan.net/dataviz/r/psych/2015/09/26/violin-plots/)
* [beeswarm plots](https://github.com/eclarke/ggbeeswarm)
* [letter-value box-plots](https://github.com/hadley/lvplot)
* Another package that makes flipping the axes easier in ggplot -- [rotating axes](https://github.com/lionel-/ggstance)


## Histograms (1)

What don't we like about this plot?

```{r bbbb3, fig.height=4, fig.width=8, cache=TRUE}
# plot code for histograms

flights %>% 
    ggplot(aes(x = dep_delay)) +
    facet_wrap(~carrier) +
    geom_histogram(bins = 150, color = "white") +
    coord_cartesian(xlim = c(-50, 250)) +
    theme_bw()

```


## Histograms (2)

* What changed in this histogram?
* What don't we like about this plot?

```{r bbbbb5, fig.height=4, fig.width=8, cache=TRUE}
# plot code for histograms

flights %>% 
    ggplot(aes(x = dep_delay)) +
    facet_wrap(~carrier, scales = "free_y") +
    geom_histogram(bins = 150, color = "white") +
    coord_cartesian(xlim = c(-50, 250)) +
    theme_bw()

```

## Boxplots

* What don't we like about this plot?
* How hard is it to explain?

```{r bbbbbb444, fig.height=4, fig.width=8, cache=TRUE}

gg_base <- flights %>%
  ggplot(aes(x = carrier, y = dep_delay)) +
    coord_cartesian(ylim = c(-50, 250)) +
    theme_bw()
# Now add varied distribution geoms

gg_base + geom_boxplot() 
```

## Violin plots

* What don't we like about this plot?
* How hard is it to explain?

```{r violin, fig.height=4, fig.width=8, cache=TRUE}

# Now add varied distribution geoms
gg_base + geom_violin()

```

## Beeswarm plots (1)

* What don't we like about this plot?
* How hard is it to explain?

```{r beeswarm, fig.height=4, fig.width=8, cache=TRUE}
# Now add varied distribution geoms
library(ggbeeswarm)
gg_base + geom_quasirandom(alpha = .6, size = .75)

```

## Beeswarm plots (1)

* What don't we like about this plot?
* How hard is it to explain?

```{r beeswarm2, fig.height=4, fig.width=8, cache=TRUE}
# Now add varied distribution geoms
library(ggbeeswarm)
gg_base + geom_quasirandom(alpha = .6, size = .75) + 
  coord_cartesian(ylim = c(-50, 500)) 

```

## Letter-Value boxplots (1)

* What don't we like about this plot?
* How hard is it to explain?

```{r lettervalue, fig.height=4, fig.width=8, cache=TRUE}
# Now add varied distribution geoms
library(lvplot)
gg_base + geom_lv(aes(fill = ..LV..)) + scale_fill_lv()

```

## Letter-Value boxplots (2)

* What don't we like about this plot?
* How hard is it to explain?

```{r lettervalue2, fig.height=4, fig.width=8, cache=TRUE}
# Now add varied distribution geoms
library(lvplot)
gg_base + geom_lv(aes(fill = ..LV..)) + 
  scale_fill_lv() + 
  coord_cartesian(ylim = c(-50, 500)) 

```

## Letter-Value boxplots (3)

* What don't we like about this plot?
* How hard is it to explain?

```{r lv3, fig.height=4, fig.width=8, cache=TRUE}
# Now add varied distribution geoms
library(lvplot)
gg_base + geom_lv(aes(fill = ..LV..)) + 
  scale_fill_lv() + 
  coord_cartesian(ylim = c(-50, 1500)) 

```

# Thursday Classes {data-background=#e8d725}


## Art Class

> - [What do you see?](images/sculpt.jpg){target="blank"}
> - [What about here?](images/sculpt_wide.jpg){target="blank"}

## The Wilderness of Data

<iframe width="860" height="515" src="https://www.youtube.com/embed/fTgvdTftXfI" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>


## Small Challenge

> - Use `geom_violin()` and `geom_quasirandom()` with the `nycflights13::flights` data to show some variable distributions.

```{r newname_code2, echo=FALSE, eval=FALSE}
library(ggbeeswarm)
library(lvplot)
## Example
mpg %>%
ggplot(aes(x = class, y = hwy)) +
  geom_violin() +
  labs(x = "Class", y = "Miles per gallon (highway)") +
  theme_bw()

mpg %>%
ggplot(aes(x = class, y = hwy, color = factor(round(displ)))) +
  geom_quasirandom() +
  scale_color_viridis_d() +
  labs(x = "Class", y = "Miles per gallon (highway)", color = "Displacement") +
  theme_bw() + theme(legend.position = "bottom")

```

