## Class

Reopen your `vis-for-gds` project and save this `.Rmd` to the `reports` folder. When working through the materials from this session run each code chunk separately by clicking the small green arrow beside each code chunk. By default I have set `eval=FALSE` on most code chunks, so when you knit the code chunk will not execute.

The technical element to this session involves analysing data from the 2019 UK General Election. You will work with results data recorded by Parliamentary Constituency. After importing and describing the dataset, you will generate data graphics that expose patterns in voting behaviour. You will do so by writing `ggplot2` specifications.


### Setup

The code block below points to the packages you need to do the work discussed in the session. If you have been following the instructions from  the last session, the only new package to install is [`parlitools`](https://cran.r-project.org/web/packages/parlitools/index.html).
```{r setup, echo=TRUE, eval=TRUE, message=FALSE}
library(tidyverse) # Bundle of packages for doing modern data analysis.
library(sf) # Spatial operations
library(here) # For navigating project directory

# install.packages("parlitools") # Uncomment to install
library(parlitools)
```

### Import

Installing `parlitools` brings down the 2019 UK General Election dataset, along with other consitituency-level datasets. Loading it with `library(parlitools)` makes these data available to your R session. The dataset containing 2019 UK General Election data is called `bes_2019`. This contains results data released by [House of Commons Library](https://commonslibrary.parliament.uk/research-briefings/cbp-8749/).

We also need to import geographic boundary data for Parliamentary Constituencies. This can be collected from [ONS Open Geography Portal](https://geoportal.statistics.gov.uk/). I have generated a simplified version of these geometries using [mapshaper](https://github.com/mbloch/mapshaper) and posted to a repository. Run the code below to import into your R session.

```{r import-geometries, echo=TRUE, eval=TRUE, message=FALSE}
url <- "https://www.roger-beecham.com/datasets/cons_outline.geojson"
cons_outline <- st_read(url, crs=27700)
```

To generate a version of the 2019 data limited to GB, and with the Swing variable (`swing_con_lab`) run the code below.

```{r gb-swing, echo=TRUE, eval=TRUE, message=FALSE}
data_gb <- bes_2019 %>%
  filter(region != "Northern Ireland") %>%
  mutate(
    swing_con_lab=0.5*((con_19-con_17)-(lab_19-lab_17)),
    # Recode to 0 Chorley incoming speaker,Buckingham outgoing speaker --  uncontested seat.
    swing_con_lab=if_else(constituency_name %in% c("Chorley", "Buckingham"),0,swing_con_lab)
  )
```

## Homework

### Task 1: Describe and evaluate

Complete the description table below.

| Data item      | Measurement level | Visual mark      | Visual channel  | Rank         |
|----------------|-------------------|------------------|-----------------|--------------|
| `location`     | <enter here>      |  <enter here>    | <enter here>    | <enter here> |
| `...`          |`...`              |  `...`           | `...`           | `...`        |
| `...`          |`...`              |  `...`           | `...`           | `...`        |
| `...`          |`...`              |  `...`           | `...`           | `...`        |
| `...`          |`...`              |  `...`           | `...`           | `...`        |
| `...`          |`...`              |  `...`           | `...`           | `...`        |


### Task 2: Reproduce


#### Histograms faceted by region


```{r hist-region-code, echo=TRUE, eval=FALSE}
#######################
# Enter your code in the chunk provided.
######################
```



#### Scatterplots with multiple encodings


```{r scatters-code, echo=TRUE, eval=FALSE}
#######################
# Enter your code in the chunk provided.
######################
```


### Task 3: Create a map


```{r map-code, echo=TRUE, eval=FALSE}
#######################
# Enter your code in the chunk provided.
######################
```
