1

I'm intending to analyse Australian census data using googleway to produce heat maps.

My approach has been to use prepared data from googleway melbourne which contains column SA2_NAME and join it with the ESRI shape file from the census data after conversion with rgdal (code below). The problem is that joining by SA2_NAME is not unique for polylines - some SA2 areas are made of multiple 'sub' areas. So it seems this is not a good approach.

A better approach would be to convert the ESRI shape data sa2_shape below to have polylines in the format of the melbourne data. How is this done?

Code below produces a 'bridging' data frame to use in joining melbourne data from googleway with ABS data which has SA2_MAIN as the key field - as stated above, the problem with this 'hack' approach is that polylines are not unique by SA2_NAME

library(tidyverse)
library(googleway)
library(rgdal)

shape_path <- "abs_data/sa2_esri_shapefile"
shape_file <- "SA2_2016_AUST"
sa2_shape <- readOGR(shape_path, shape_file)
sa2_df <- data.frame(sa2_shape$SA2_MAIN, sa2_shape$SA2_NAME)
names(sa2_df) <- c("SA2_MAIN", "SA2_NAME")
sa2_df <- sa2_df %>% semi_join(melbourne, by = "SA2_NAME")
Vlad
  • 3,058
  • 4
  • 25
  • 53
  • 1
    If you use `sf` objects they will naturally get plotted by `googleway`. Use `library(sf); sf <- sf::st_read( 'path_to_shape_file.shp' )` to load your data as `sf` objects. – SymbolixAU Aug 29 '18 at 08:09
  • Or, if you're using `sp` objects, convert them to `sf` using `sf::st_as_sf()` – SymbolixAU Aug 29 '18 at 08:11
  • @SymbolixAU This works fine - also please note it seems google_map() crashes if the geometry column (from using sf) is an empty list. As a work around, this can be fixed by filtering these out. – Vlad Aug 29 '18 at 12:54
  • 1
    You may be interested in the ASGS package [Note: 700MB] (for mapping SA2s): `library(ASGS.foyer); install_ASGS()` and the `Census2016.DataPack` package: https://github.com/hughparsonage/Census2016.Datapack – Hugh Aug 29 '18 at 13:35
  • " crashes if the geometry column (from using sf) is an empty list " - are you able to give an example and add it to [the issue tracker](https://github.com/SymbolixAU/googleway/issues) ? I can't seem to generate this error. – SymbolixAU Aug 29 '18 at 21:54
  • OK - added https://github.com/SymbolixAU/googleway/issues/176 – Vlad Aug 30 '18 at 00:07

1 Answers1

1

As per SymbolixAU comment - used sf to load the data and this works as long as geometry is not an empty list - see code below.

library(tidyverse)
library(googleway)
library(sf)

shape_path <- "abs_data/sa2_esri_shapefile"
shape_file <- "SA2_2016_AUST"

shape_file_path <- paste0(shape_path, "/", shape_file, '.shp')
sa2_shape <- sf::st_read(shape_file_path)
sa2_shape <- sa2_shape %>% 
  filter(STATE_NAME == "Victoria",
         AREA_SQKM > 0)# This is important - otherwise google_map() will crash!
google_map() %>% 
  googleway::add_polygons(data = sa2_shape, 
                          polyline = "geometry", 
                          fill_colour = "SA2_NAME")

Victoria SA2 map

> sa2_shape %>% head()
Simple feature collection with 6 features and 6 fields
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: 143.6849 ymin: -37.68153 xmax: 143.951 ymax: -37.46847
epsg (SRID):    4283
proj4string:    +proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs
   SA2_MAIN SA2_MAIN16         SA2_NAME STATE_CODE STATE_NAME AREA_SQKM                       geometry
1 201011001  201011001        Alfredton          2   Victoria   52.7111 MULTIPOLYGON (((143.7072 -3...
2 201011002  201011002         Ballarat          2   Victoria   12.3787 MULTIPOLYGON (((143.8675 -3...
3 201011003  201011003 Ballarat - North          2   Victoria   92.3577 MULTIPOLYGON (((143.853 -37...
4 201011004  201011004 Ballarat - South          2   Victoria   32.8541 MULTIPOLYGON (((143.8675 -3...
5 201011005  201011005        Buninyong          2   Victoria   51.5855 MULTIPOLYGON (((143.8533 -3...
6 201011006  201011006        Delacombe          2   Victoria   34.1608 MULTIPOLYGON (((143.7072 -3...
Vlad
  • 3,058
  • 4
  • 25
  • 53