## 🤺 Exercise 5A

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


```{r election-data, eval = TRUE, message = FALSE}
url = "https://results.aec.gov.au/27966/Website/Downloads/HouseDopByDivisionDownload-27966.csv"

election_data <- read_csv(url, skip = 1)
  
election_data = election_data |>
  filter(CalculationType == "Preference Count" &
    Elected == "Y" &
    CountNumber == 0) |>
  mutate(DivisionNm = toupper(DivisionNm))
```

```{r vic-map}
data_path = "data/vic-july-2021-esri/E_VIC21_region.shp" 

vic_election_map <- read_sf(here::here(data_path)) %>%
  mutate(DivisionNm = toupper(Elect_div))

vic_election_map = vic_election_map |>
  left_join(election_data, by = "DivisionNm")
```

```{r party-colors, eval = TRUE}
party_colors <- c(
  "ALP" = "#DE3533",
  "GVIC" = "#10C25B",
  "IND" = "#000000",
  "LP" = "#0047AB",
  "NP" = "#FFFF00"
)
```
a)

```{r vicmap19}
ggplot(vic_election_map) +
   geom_sf(aes(geometry = geometry, fill = PartyAb), 
                  color = "white") + 
   coord_sf(xlim = c(144.8, 145.2), ylim = c(-38.1, -37.6)) +
   scale_fill_manual(values = party_colors) + 
   ggtitle("Winners of Australian Federal Election in 2019", 
           subtitle = "Victoria")
```

b) 

```{r crop-map, warning = FALSE, message = FALSE}
crop_map <- vic_election_map %>% 
  st_crop(xmin = 144.8, xmax = 145.2,
          ymin = -38.1, ymax = -37.6)

plot_vic_electorates_cropped <- ggplot(crop_map) +
   geom_sf(aes(geometry = geometry, fill = PartyAb), 
                  color = "white") + 
   ggtitle("Winners of Australian Federal Election in 2019", 
           subtitle = "Victoria") +
  scale_fill_manual(values = party_colors[unique(crop_map$PartyAb)]) 

plot_vic_electorates_cropped
```


c) 

```{r map-with-labels, warning = FALSE, message = FALSE}
data_for_labels = crop_map %>% 
      filter(Elect_div %in% c("Melbourne", "Menzies", "Macnamara"))

plot_vic_electorates_cropped +
  geom_sf_label(data = data_for_labels,
    aes(label = Elect_div, geometry = geometry),
    size = 2)
```

## 🗺 Exercise 5B 


```{r}
data(world, package = "spData")
```

a. 

```{r world-map}
ggplot(world) + geom_sf()
```

b. 

```{r moll}
world %>% 
  st_transform(crs = "+proj=moll") %>% 
  ggplot() + geom_sf()
```

c. 

```{r laea}
world %>% 
  st_transform(crs = "+proj=laea +x_0=0 +y_0=0 +lon_0=133.78 +lat_0=-25.27") %>% 
  ggplot() + geom_sf()
```

####  Material mainted and updated by Dr. Kate Saunders. Material orginally developed by Dr. Emi Tanaka.
