Title: | Create Tessellated Hexagon Maps |
---|---|
Description: | Create a hexagon tile map display from spatial polygons. Each polygon is represented by a hexagon tile, placed as close to it's original centroid as possible, with a focus on maintaining spatial relationship to a focal point. Developed to aid visualisation and analysis of spatial distributions across Australia, which can be challenging due to the concentration of the population on the coast and wide open interior. |
Authors: | Dianne Cook [aut, cre, ths] , Stephanie Kobakian [aut] , Matt Cowgill [ctb] |
Maintainer: | Dianne Cook <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.8 |
Built: | 2024-11-04 03:49:12 UTC |
Source: | https://github.com/srkobakian/sugarbag |
Chooses a hexagon centroid for each polygon in the shape file, from a grid spanning the longitudes and latitudes in the expanded bounding box.
allocate( centroids, hex_grid, sf_id = names(centroids)[1], hex_size, hex_filter, focal_points = NULL, order_sf_id = NULL, width = 30, verbose )
allocate( centroids, hex_grid, sf_id = names(centroids)[1], hex_size, hex_filter, focal_points = NULL, order_sf_id = NULL, width = 30, verbose )
centroids |
a data frame with centroids of non empty polygons |
hex_grid |
a data frame containing all possible hexagon points |
sf_id |
a string to indicate the column to identify individual polygons |
hex_size |
a float value in degrees for the diameter of the hexagons |
hex_filter |
amount of hexagons around centroid to consider |
focal_points |
a data frame of reference locations when allocating hexagons, capital cities of Australia are used in the example |
order_sf_id |
a string to indicate the column used to order polygons |
width |
a numeric indicating the angle used to filter the hexagon grid |
verbose |
a boolean to indicate whether to show polygon id |
a data frame of all allocated hexagon points
# Create centroids set centroids <- create_centroids(tas_lga, sf_id = "lga_code_2016") # Smaller set for faster example centroids <- centroids[1:10,] # Create hexagon location grid data(capital_cities) grid <- create_grid(centroids = centroids, hex_size = 0.2, buffer_dist = 1.2) # Allocate polygon centroids to hexagon grid points hex_allocated <- allocate( centroids = centroids, hex_grid = grid, hex_size = 0.2, # same size used in create_grid hex_filter = 3, focal_points = capital_cities, width = 30, verbose = TRUE ) # NEXT: # create a set of hexagon points for plotting # using fortify_hexagon, and # plot the hexagons with geom_polygon, see vignette
# Create centroids set centroids <- create_centroids(tas_lga, sf_id = "lga_code_2016") # Smaller set for faster example centroids <- centroids[1:10,] # Create hexagon location grid data(capital_cities) grid <- create_grid(centroids = centroids, hex_size = 0.2, buffer_dist = 1.2) # Allocate polygon centroids to hexagon grid points hex_allocated <- allocate( centroids = centroids, hex_grid = grid, hex_size = 0.2, # same size used in create_grid hex_filter = 3, focal_points = capital_cities, width = 30, verbose = TRUE ) # NEXT: # create a set of hexagon points for plotting # using fortify_hexagon, and # plot the hexagons with geom_polygon, see vignette
A dataset containing the longitude and latitude values of Australian capital cities.
capital_cities
capital_cities
A data frame with 8 rows and 3 variables:
name of cities
location of point in longitude degrees
location of point in latitude degrees
For one row of an sf data frame, calculate the distance to the closest focal point. Return the name of the focal point, and the angle between focal point and centroid.
closest_focal_point(centroid, focal_points)
closest_focal_point(centroid, focal_points)
centroid |
a data frame describing one centroid |
focal_points |
a data frame of the longitude and latitude values |
data frame containing the name and location of the closest focal
# Create a set of polygon centroids centroids <- create_centroids(tas_sa2, "sa2_5dig_2016") # Find the closest capital city for the first centroid closest_focal_point(centroids[1, ], capital_cities)
# Create a set of polygon centroids centroids <- create_centroids(tas_sa2, "sa2_5dig_2016") # Find the closest capital city for the first centroid closest_focal_point(centroids[1, ], capital_cities)
Called from within create_grid function, this function takes the bounding box of a group of polygons, or a specific table of minimum and maximum longitudes and latitudes to create points for each polygon to be allocated to that will tessellate into hexagons.
create_buffer(centroids, grid, hex_size, buffer_dist, verbose = FALSE)
create_buffer(centroids, grid, hex_size, buffer_dist, verbose = FALSE)
centroids |
data frame of centroids to be allocated |
grid |
data frame of hexagon centroids |
hex_size |
a float value in degrees for the diameter of the hexagons |
buffer_dist |
distance to extend beyond the geometry provided |
verbose |
a boolean to indicate whether to show function progress |
data frame of hexagon centroids
lga_centroids <- create_centroids(sugarbag::tas_lga, "lga_code_2016") lga_grid <- create_grid(lga_centroids, hex_size = 0.2, buffer_dist = 1.2)
lga_centroids <- create_centroids(sugarbag::tas_lga, "lga_code_2016") lga_grid <- create_grid(lga_centroids, hex_size = 0.2, buffer_dist = 1.2)
Create a data frame of longitude and latitude centroids of each polygon.
create_centroids(shp_sf, sf_id, largest = TRUE, verbose = FALSE)
create_centroids(shp_sf, sf_id, largest = TRUE, verbose = FALSE)
shp_sf |
an sf object, a data set with a simple feature list column |
sf_id |
a string to indicate the column to identify individual polygons |
largest |
logical; for |
verbose |
a boolean to indicate whether to show function progress |
a tibble containing longitude and latitude
centroids <- create_centroids(tas_lga, "lga_code_2016")
centroids <- create_centroids(tas_lga, "lga_code_2016")
This function takes the output from the create_centroids function, or a set of centroids in a table with the columns latitude and longitude
create_grid( centroids, hex_size, buffer_dist, latitude = "latitude", longitude = "longitude", verbose = FALSE )
create_grid( centroids, hex_size, buffer_dist, latitude = "latitude", longitude = "longitude", verbose = FALSE )
centroids |
data frame of centroids to be allocated, this should have columns for longitude and latitude value of centroids, as |
hex_size |
a float value in degrees for the diameter of the hexagons |
buffer_dist |
distance to extend beyond the geometry provided |
latitude |
the column name for the latitude values of the centroids |
longitude |
the column name for the longitude values of the centroids |
verbose |
a boolean to indicate whether to show function progress |
grid
# Create a set of centroids for grid to overlay centroids <- create_centroids(tas_lga, "lga_code_2016") # Create the grid grid <- create_grid(centroids = centroids, hex_size = 0.2, buffer_dist = 1.2, verbose = FALSE)
# Create a set of centroids for grid to overlay centroids <- create_centroids(tas_lga, "lga_code_2016") # Create the grid grid <- create_grid(centroids = centroids, hex_size = 0.2, buffer_dist = 1.2, verbose = FALSE)
Allocates each polygon in a shape file to a grid point to create a map of tessellated hexagons. The spatial relationships of areas are preserved while the geographic shape of each area is lost.
create_hexmap( shp, sf_id, hex_size = NULL, buffer_dist = NULL, hex_filter = 10, f_width = 30, focal_points = NULL, order_sf_id = NULL, export_shp = FALSE, verbose = FALSE )
create_hexmap( shp, sf_id, hex_size = NULL, buffer_dist = NULL, hex_filter = 10, f_width = 30, focal_points = NULL, order_sf_id = NULL, export_shp = FALSE, verbose = FALSE )
shp |
a shape file, if class is SPDF, will be converted to sf |
sf_id |
name of a unique column that distinguishes areas |
hex_size |
a float value in degrees for the diameter of the hexagons |
buffer_dist |
distance in degrees to extend beyond the geometry provided |
hex_filter |
amount of hexagons around centroid to consider |
f_width |
the angle used to filter the grid points around a centroid |
focal_points |
a data frame of reference locations when allocating hexagons, capital cities of Australia are used in the example |
order_sf_id |
a string name of a column to order by for allocating |
export_shp |
export the simple features set |
verbose |
a boolean to indicate whether to show function progress |
a data set containing longitude and latitude of allocated hexagon points for each non null geometry passed in the shape file
data(tas_lga) # Smaller set for faster example tas_lga_sub <- tas_lga[1:10,] data(capital_cities) hexmap <- create_hexmap( shp = tas_lga_sub, sf_id = "lga_code_2016", hex_filter = 3, focal_points = capital_cities, verbose = TRUE)
data(tas_lga) # Smaller set for faster example tas_lga_sub <- tas_lga[1:10,] data(capital_cities) hexmap <- create_hexmap( shp = tas_lga_sub, sf_id = "lga_code_2016", hex_filter = 3, focal_points = capital_cities, verbose = TRUE)
Takes only the closest available gridpoints as possible hexagon centroids to allocate polygons.
filter_grid_points( f_grid, f_centroid, focal_points = NULL, f_dist = filter_dist, angle_width = width, h_size = hex_size )
filter_grid_points( f_grid, f_centroid, focal_points = NULL, f_dist = filter_dist, angle_width = width, h_size = hex_size )
f_grid |
complete grid of hexagon centroids |
f_centroid |
the longitude and latitude values for the current centroid |
focal_points |
a tibble of focal locations, an optional argument that allows allocation of polygons to hexagon centroids in ascending order of the distance to the closest focal point. It also filters the grid points to those within a 30 degree range of the angle from focal point to centroid. The default "capitals" uses the locations of the Australian capital cities as focal points. |
f_dist |
a distance in degrees, used as a boundary to filter the hexagon centroids considered for each polygon centroid to be allocated. |
angle_width |
a numeric used to filter the hexagon grid |
h_size |
a float value in degrees for the diameter of the hexagons |
a tibble of filtered grid points
Creates the points that define a hexagon polygon for plotting
fortify_hexagon(data, sf_id, hex_size)
fortify_hexagon(data, sf_id, hex_size)
data |
a data frame created by the allocate function |
sf_id |
a string to indicate the column to identify individual polygons |
hex_size |
a float value in degrees for the diameter of the hexagons |
a data frame of the seven points used to draw a hexagon
# same column is used in create_centroids fortify_hexagon(data = tas_lga_hexctr, sf_id = "lga_code_2016", hex_size = 0.2)
# same column is used in create_centroids fortify_hexagon(data = tas_lga_hexctr, sf_id = "lga_code_2016", hex_size = 0.2)
This will contain individual points for plotting the polygon, indicating the longitude and latitude, order of points, if a hole is present, the piece, id and group.
fortify_sfc(sfc_df, keep = NULL)
fortify_sfc(sfc_df, keep = NULL)
sfc_df |
a simples features data set |
keep |
ratio of points to keep |
a tibble point of long lat points used to plot polygons
A dataset containing first preference vote counts, candidate names, and other results for the House of Representatives from the 2016 Australian federal election. The data were obtained from the Australian Electoral Commission, and downloaded from https://results.aec.gov.au/24310/Website/Downloads/HouseFirstPrefsByPartyDownload-24310.csv
fp19
fp19
A data frame with the following variables:
StateAbAbbreviation for state name
UniqueIDnumeric identifier that links the electoral division with Census and other election datasets.
DivisionNmElectoral division name
BallotPositionCandidate's position on the ballot
CandidateIDCandidate ID
SurnameCandidate surname
GivenNmCandidate given name
PartyAbAbbreviation for political party name
PartyNmPolitical party name
ElectedWhether the candidate was elected (Y/N)
HistoricElectedWhether the candidate is the incumbent member
OrdinaryVotesNumber of ordinary votes cast at the electorate for the candidate
PercentPercentage of ordinary votes for the candidate
geom_sugarbag()
provides a convenient way to create tesselated
hexagon maps using the sugarbag algorithm.
geom_sugarbag( mapping = NULL, data = NULL, stat = "sugarbag", position = "identity", hex_size = 0.2, na.rm = FALSE, ... )
geom_sugarbag( mapping = NULL, data = NULL, stat = "sugarbag", position = "identity", hex_size = 0.2, na.rm = FALSE, ... )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this
layer, either as a |
position |
Position adjustment, either as a string naming the adjustment
(e.g. |
hex_size |
Default is 0.2. Units are degrees, corresponding to
the diameter of the hexagons. See |
na.rm |
If |
... |
Other arguments passed on to |
Create a sugarbag hex map
The sugarbag algorithm creates a hexagon tile map from spatial polygons. It represents each polygon with a hexagon, which is placed close to the polygon's centroid while also maintaining its spatial relationship to a focal point.
If geom_sugarbag()
is used to make a map of Australia, the capital cities
will be used as focal points. For non-Australian maps, a single focal point
will be inferred from the data, as the centroid with the smallest total
distance to its three nearest neighbours. To specify focal points manually,
construct your hexagon grid manually – see ?allocate
.
allocate, ggplot2::geom_polygon
library(ggplot2) # Map of Tasmanian local govt areas using built-in data tas_lga %>% ggplot(aes(fill = lga_name_2016)) + geom_sf(alpha = 0.1) + geom_sugarbag(aes(geometry = geometry)) + theme(legend.position = "none") # Map of SIDS data from North Carolina if (requireNamespace("sf", quietly = TRUE)) { nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) ggplot(nc, aes(fill = AREA)) + geom_sf(alpha = 0.1) + geom_sugarbag(aes(geometry = geometry)) }
library(ggplot2) # Map of Tasmanian local govt areas using built-in data tas_lga %>% ggplot(aes(fill = lga_name_2016)) + geom_sf(alpha = 0.1) + geom_sugarbag(aes(geometry = geometry)) + theme(legend.position = "none") # Map of SIDS data from North Carolina if (requireNamespace("sf", quietly = TRUE)) { nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) ggplot(nc, aes(fill = AREA)) + geom_sf(alpha = 0.1) + geom_sugarbag(aes(geometry = geometry)) }
A data frame of the Statistical Area at Level 2 names and amount of homeless
homeless
homeless
A data frame with 545 rows and 2 variables:
amount of homeless people
name of the Statistical Area at Level 2
read_shape
read_shape(shp_path, simplify = TRUE, keep = 0.1)
read_shape(shp_path, simplify = TRUE, keep = 0.1)
shp_path |
character vector location of shape file, extension .shp |
simplify |
a boolean to decide whether to simplify the shape file using rmapshaper, keeping all shapes. |
keep |
ratio of points to keep |
an sf data frame, with a column of non null geometries
# Example of how a shape file is read if (interactive()) { shape <- read_shape(shp_path = file.choose()) }
# Example of how a shape file is read if (interactive()) { shape <- read_shape(shp_path = file.choose()) }
A simple features dataset containing the polygons for all Australian LGAs in 2016.
tas_lga
tas_lga
A simple features data frame with 39 rows and 6 variables:
code for the Local Government Area
name of the Local Government Area
code for the state containing the Local Government Area
name of the state containing the Local Government Area
area contained in the polygon
describes where on Earth the polygon is located
A tibble dataset containing the processed data for all Australian LGAs in 2016. Each point corresponds to hexagon centre.
tas_lga_hexctr
tas_lga_hexctr
A simple features data frame with 39 rows and 6 variables:
code for the Local Government Area
polygon centroid
Focal point (capital city) information used for each polygon/hexagon
row number, in case it can be useful
hexagon centre and id
A simple features dataset containing the polygons for all Tasmanian SA2s in 2016.
tas_sa2
tas_sa2
A simple features data frame with 99 rows and 15 variables:
complete code of the Statistical Area
simple code for the Statistical Area
name of the Statistical Area
code for the SA3 containing the Statistical Area
name of the SA3 containing the Statistical Area
code for the SA4 containing the Statistical Area
name of the SA4 containing the Statistical Area
code for the Greater Capital City region containing the Statistical Area
name of the Greater Capital City region containing the Statistical Area
code for the state containing the Statistical Area
name of the state containing the Statistical Area
area contained in the polygon
distinguishes SA2 regions
amount of people living within the region
code of the Statistical Area