GGPLOT2

## ── Attaching packages ────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 2.2.1     ✔ purrr   0.2.4
## ✔ tibble  1.4.2     ✔ dplyr   0.7.4
## ✔ tidyr   0.8.0     ✔ stringr 1.3.0
## ✔ readr   1.1.1     ✔ forcats 0.2.0
## ── Conflicts ───────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()

Recap from Yesterday

Wrangling Data

  • “verbs” from the tidyverse
    • select()
    • filter()
    • group_by()
    • summarise()
## Parsed with column specification:
## cols(
##   .default = col_integer(),
##   wday = col_character(),
##   temperature = col_double(),
##   lactate = col_double()
## )
## See spec(...) for full column specifications.
## # A tibble: 6 x 5
##   hospital patient dead28 icu_accept icu_admit
##      <int>   <int>  <int>      <int>     <int>
## 1        1    2750      1          0         0
## 2        1    2297      1          1         1
## 3        1    3782      0          1         1
## 4        1    2337      0          0         1
## 5        1    1020      0          0         0
## 6        1    4852      0          0         0

Are patients sicker at weekends?

## # A tibble: 2 x 3
##   weekend mean.sofa sd.sofa
##   <lgl>       <dbl>   <dbl>
## 1 FALSE        4.02    2.41
## 2 TRUE         4.13    2.30

A better way?

## # A tibble: 2 x 3
##   weekend mean.sofa sd.sofa
##   <lgl>       <dbl>   <dbl>
## 1 FALSE        4.02    2.41
## 2 TRUE         4.13    2.30

The Grammar of Graphics

Building Layers

  • The Data
    • The thing we are plotting
  • Mapping Aesthetics
    • How we are mapping the data to a visual dimention
  • Geometric Objects
    • How the mapping is presented to us

The Data

  • Should be “tidy”
  • Think carefully about unit of observation

Mapping Aesthetics

  • This is how the data is “mapped” to a visual dimention
  • We are most familiar with x and y mappings
  • Others might include:
    • Size
    • Shape
    • Colour/Fill

Geometric Objects

  • This allows us to display our data
  • Points, lines, bars etc.