-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
98 lines (70 loc) · 2.38 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# hexmap
<!-- badges: start -->
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)
<!-- badges: end -->
The hexmap R package aims to automate the conversion of geospatial polygons into hexagonal grids. This conversion allows users to better visualise statistical values associated with each geospatial region.
## Installation
You can install the development version of hexmap like so:
``` r
install.packages("remotes")
remotes::install_github("numbats/hexmap")
```
## Method
1. Make a cartogram so that each region has an approximately equal area (optional)
2. Create a hexagonal grid based on this cartogram or other geospatial object
3. Assign the region to the "closest" hexagon
## Example: Australian federal election 2022
```{r, message = FALSE}
library(hexmap)
library(sf)
library(cartogram)
library(ggplot2)
library(dplyr)
set.seed(1)
winner_map <- function(data) {
party_colors <- c("ALP" = "red3", "LNP" = "blue3", "GRN" = "seagreen", "Other" = "yellow2")
ggplot(data) +
geom_sf(aes(fill = winner)) +
theme_void() +
scale_fill_manual(values = party_colors)
}
state_map <- function(data) {
ggplot(data) +
geom_sf(aes(fill = state)) +
theme_void()
}
```
The data `aec2022` in hexmap is an `sf` object with a simplified Australian federal electoral boundary and the corresponding party that won the 2022 Australian federal election for that electoral division.
### Choropleth map
```{r plot-choropleth, message = FALSE}
winner_map(aec2022)
```
### Cartogram
```{r plot-cartogram, message = FALSE, warning = FALSE, cache = TRUE}
aec2022carto <- aec2022 %>%
st_transform(crs = "+init=epsg:3112") %>%
mutate(weight = 1) %>%
cartogram_cont("weight", itermax = 50)
winner_map(aec2022carto)
state_map(aec2022carto)
```
### `geogrid` hexagonal map
```{r plot-geogrid, warning = FALSE, cache = TRUE}
hex <- geogrid::calculate_grid(aec2022carto, grid_type = "hexagonal")
res <- geogrid::assign_polygons(aec2022carto, hex)
state_map(res)
```
## Related
- [`geogrid`](https://github.com/jbaileyh/geogrid) package