knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" ) devtools::load_all() library(sf) library(tidyverse) options(width = 1000)
:warning: This package is not owned, run, or endorsed by the ABS. Data contained in this package are compressed, re-projected, renamed and stored as
sf
objects to be useful for making maps in R. If conducting spatial analysis, or any analysis that requires precise area boundaries, please use the original shapefiles provided by the ABS and others.
absmapsdata
is a user-generated package to make it easier for R users to access ABS (and other) spatial structure names/codes and produce maps using this data. The package contains compressed (lossy), tidied, and lazily-loadable sf
objects that hold geometric information about data structures in Australia.
It also contains a correspondences files provided by the ABS.
:white_check_mark: It is now recommended that you use
strayr::read_absmap
to access data stored inabsmapsdata
. To download and read these data files without installing the wholeabsmapsdata
package, please usestrayr::read_absmap
, for example:
# remotes::install_github("runapp-aus/strayr") strayr::read_absmap("sa42021")
You probably don't need to install the full absmapsdata
package (see above).
But if you want to, you can install absmapsdata
from Github.
The package contains a lot of data, so installing using remotes::install_github
may fail if the download times out. If this happens, set the timeout option to a large value and try again, i.e. run:
options(timeout = 1000) remotes::install_github("wfmackey/absmapsdata")
The sf
package is required to handle the sf
objects:
library(sf)
Available maps are listed below. These will be added to over time. If you would like to request a map to be added, let me know via an issue on this Github repo.
ASGS Main Structures
sa12011
; 2016: sa12016
; and 2021: sa12021
. sa22011
; 2016: sa22016
; and 2021: sa22021
. sa32011
; 2016: sa32016
; and 2021: sa32021
. sa42011
; 2016: sa42016
; and 2021: sa42021
. gcc2011
; 2016: gcc2016
; and 2021: gcc2021
. ra2011
; and 2016: ra2016
state2011
; 2016: state2016
; and state2021
.ASGS Indigenous Structures
iloc2021
iare2021
ireg2021
Significant Urban Areas and Urban Centres and Localities
sua2016
; and 2021: sua2021
ucl2016
; and 2021: ucl2021
sosr2016
sos2016
ASGS Non-ABS Structures
ced2018
; and 2021: ced2021
sed2018
; 2021: sed2021
; and 2022: sed2022
lga2016
; 2018: lga2018
; 2021: lga2021
; and 2022: lga2022
regional_ivi2008
postcode2016
; and 2021: postcode2021
suburb2016
; and (SAL) 2021: suburb2021
dz2011
; 2016: dz2016
; and 2021: dz2021
.Non-ABS Australian Government Structures
employment_regions2015
bitre_work_zones2016
nsw_lhd2023
rda2016
Correspondences
This package also contains a number of 2016 population-weighted ABS correspondences (the most recent) that can be found on the data.gov.au website.
:white_check_mark: Use
strayr::read_correspondence_tbl
to access correspondence this data, rather than loading the wholeabsmapsdata
package, e.g.:
# remotes::install_github("runapp-aus/strayr") strayr::read_correspondence_tbl(from_area = "sa2", from_year = 2011, to_area = "sa2", to_year = 2016)
Within absmapsdata
, you can retrieve correspondences with the get_correspondence_absmaps
function.
The absmapsdata
package comes with pre-downloaded and pre-processed
data. To load a particular geospatial object: load the package, then
call the object (see list above for object names).
library(tidyverse) library(sf) library(absmapsdata) mapdata1 <- sa32021 glimpse(mapdata1)
Or
mapdata2 <- sa22016 glimpse(mapdata2)
The resulting sf
object contains one observation per area (in the
following examples, one observation per sa3
). It stores the geometry
information in the geometry
variable, which is a nested list
describing the area’s polygon. The object can be joined to a standard
data.frame
or tibble
and can be used with dplyr
functions.
sf
objectWe do all this so we can create gorgeous maps. And with the sf
object
in hand, plotting a map via ggplot
and geom_sf
is simple.
map <- sa32016 %>% filter(gcc_name_2016 == "Greater Melbourne") %>% # let's just look Melbourne ggplot() + geom_sf(aes(geometry = geometry)) # use the geometry variable map
The data also include centroids of each area, and we can add these
points to the map with the cent_lat
and cent_long
variables using
geom_point
.
map <- sa32016 %>% filter(gcc_name_2016 == "Greater Melbourne") %>% # let's just look Melbourne ggplot() + geom_sf(aes(geometry = geometry)) + # use the geometry variable geom_point(aes(cent_long, cent_lat)) # use the centroid long (x) and lats (y) map
Cool. But this all looks a bit ugly. We can pretty it up
using ggplot
tweaks. See the comments on each line for its objective.
Also note that we’re filling the areas by their areasqkm
size, another
variable included in the sf
object (we’ll replace this with more
interesting data in the next section).
map <- sa32016 %>% filter(gcc_name_2016 == "Greater Melbourne") %>% # let's just look Melbourne ggplot() + geom_sf(aes(geometry = geometry, # use the geometry variable fill = areasqkm_2016), # fill by area size lwd = 0, # remove borders show.legend = FALSE) + # remove legend geom_point(aes(cent_long, cent_lat), # use the centroid long (x) and lats (y) colour = "white") + # make the points white theme_void() + # clears other plot elements coord_sf() map
At some point, we’ll want to join our spatial data with data-of-interest. The variables in our mapping data—stating the numeric code and name of each area and parent area—will make this relatively easy.
For example: suppose we had a simple dataset of median income by SA3 over time.
# Read data in some data income <- read_csv("https://raw.githubusercontent.com/wfmackey/absmapsdata/master/img/data/median_income_sa3.csv") head(income)
This income data contains a variable sa3_name_2016
, and we can use
dplyr::left_join()
to combine with our mapping data.
combined_data <- left_join(income, sa32016, by = "sa3_name_2016")
Now that we have a tidy dataset with 1) the income data we want to plot, and 2) the geometry of the areas, we can plot income by area:
map <- combined_data %>% filter(gcc_name_2016 == "Greater Melbourne") %>% # let's just look Melbourne ggplot() + geom_sf(aes(geometry = geometry, # use the geometry variable fill = median_income), # fill by unemployment rate lwd = 0) + # remove borders theme_void() + # clears other plot elements labs(fill = "Median income") map
:white_check_mark: Use
strayr::read_correspondence_tbl
to access correspondence this data, rather than loading the wholeabsmapsdata
package, e.g.:
# remotes::install_github("runapp-aus/strayr") strayr::read_correspondence_tbl(from_area = "sa2", from_year = 2011, to_area = "sa2", to_year = 2016)
You can use the absmapsdata::get_correspondence_absmaps
function to get population-weighted correspondence tables provided by the ABS.
Note that while there are lots of correspondence tables, not every combination is available.
For example:
get_correspondence_absmaps("cd", 2006, "sa1", 2016)
The motivation for this package is that maps are cool and fun and are,
sometimes, the best way to communicate data. And making maps is R
with
ggplot
is relatively easy when you have the right object
.
Getting the right object
is not technically difficult, but requires
research into the best-thing-to-do at each of the following steps:
R
using one-of-many import tools.For me at least, finding the correct information and developing the
best set of steps was a little bit interesting but mostly tedious and
annoying. The absmapsdata
package holds this data for you, so you can
spend more time making maps, and less time on Stack Overflow, the ABS
website, and lovely-people’s wonderful
blogs.
The best avenue is via a Github issue at wfmackey/absmapsdata/issues. This is also the best place to request data that isn't yet available in the package.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.