In this tutorial, you will learn to
Note: you should do Preparation before the tutorial.
install.packages(c("sf", "spData", "terra"))
You can download this data here:
* Go to https://results.aec.gov.au * 2022 federal election *
Downloads * Distribution of preferences by candidate by division
Or directly via the url here: https://results.aec.gov.au/27966/Website/Downloads/HouseDopByDivisionDownload-27966.csv
# for speeding up geom_sf
if (!identical(getOption("bitmapType"), "cairo") && isTRUE(capabilities()[["cairo"]])) {
options(bitmapType = "cairo")
}
Download, combine and wrangle the election data so it is ready for mapping!. We will need this for exercise part 5B.
library(tidyverse)
library(sf)
url = "https://results.aec.gov.au/27966/Website/Downloads/HouseDopByDivisionDownload-27966.csv"
election_data <- read_csv(url,
skip = 1
)
Use the help menu ?read_csv
or ask your tutor why we
need to set skip=1
.
election_data = election_data |>
# Select winning candidate only
filter(CalculationType == "Preference Count" &
Elected == "Y" &
CountNumber == 0) |>
# make division name upper case so
# it can be joined later with map data
mutate(DivisionNm = toupper(DivisionNm))
Again look a the help menu to see examples of how
?toupper
works.
read_csv
here. We need to use read_sf
as this
is a map.data_path = "data/vic-july-2021-esri/E_VIC21_region.shp"
vic_election_map <- read_sf(here::here(data_path)) %>%
# to match up with election data
mutate(DivisionNm = toupper(Elect_div))
vic_election_map = vic_election_map |>
left_join(election_data, by = "DivisionNm")
You can refresh how to combine to data sets together by reviewing the tidyverse cheat sheet.
How many and which political parties do we need colours for?
election_data |>
filter(StateAb == "VIC") |>
select(PartyAb) |>
distinct(PartyAb)
## # A tibble: 5 × 1
## PartyAb
## <chr>
## 1 LP
## 2 ALP
## 3 NP
## 4 IND
## 5 GVIC
Using the parliamentary handbook, we can match the political party with their associated colour.
party_colors <- c(
"ALP" = "#DE3533",
"GVIC" = "#10C25B",
"IND" = "#000000",
"LP" = "#0047AB",
"NP" = "#FFFF00"
)
You can change these if you’d like to. Many of the inner city independents used the teal colour.
Draw a map of Victoria and colour the electorate districts with the political party that won that district in the 2022 federal election.
To do this we need to use mapping functions that work with
ggplot2
.
The function geom_sf
is used to define the
aesthetics of how to plot the map.
The function coord_sf
is used to define the
coordinate range of the plot.
vic_election_map |>
ggplot() +
geom_sf(aes(geometry = ???, fill = ???), color = "black")
vic_election_map |>
ggplot() +
geom_sf(aes(geometry = geometry, fill = PartyAb), color = "black") +
coord_sf(xlim = c(???, ???), ylim = c(???, ???)) +
vic_election_map |>
ggplot() +
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"
)
You may find the function st_crop
useful here.
vic_election_map |>
st_crop(xmin = ???, xmax = ???,
ymin = ???, ymax = ???)
The function geom_sf_label
can be used to add labels to
a map.
vic_map_subset = vic_election_map %>%
filter(Elect_div %in% c("Melbourne", "Menzies", "Macnamara"))
vic_election_map |>
ggplot() +
geom_sf(aes(geometry = geometry), color = "black") +
geom_sf_label(data = vic_map_subset, aes(label = ???, geometry = ???))
Learn to modify the map projection The world is not flat. Let’s learn how to deal with that in the mapping world.
Load the world map data contained in spData
.
data(world, package = "spData")
world
using
ggplot2
.crs = "+proj=moll"
in the st_transform
function and visualise the result.## Look at the last example in the help
?geom_sf