The hexwall at useR! 2018 features roughly 200 contributed R package hexagon stickers. If you’ve ever had difficulty aligning hexagon stickers in your slides or even on your laptop, you may appreciate the challenge of arranging hundreds of hexagons. Fortunately with R and a little bit of magick, we can substantially simplify this process.
Collection
Hexagon collection proved to be one of the more time-consuming steps in the project. Without a Comprehensive R Hexagon Repository (CRHR), Di Cook (@visnut) and I resorted to collecting hexagons via email and promoting the project via twitter.
Our collection efforts were also supported by Rstudio’s hex-stickers and Bioconductor’s BiocStickers repositories. We opted not to sift through the hexb.in library as we wanted the feature wall to feature only R package stickers.
The response to our project has been huge, and we are grateful for everyone who has contributed their stickers.
hexwall
The hexwall script (available at mitchelloharawild/hexwall) provides the magick for cleaning, sorting, and arranging hexagons. The function does the following operations using the ROpenSci magick package:
Load the images
Make white backgrounds transparent
Trim images
Remove bad images (low resolution or incorrect dimensions)
To create a hexagon map of Australia, we first need to find a map. I’m using the GADM database to give a nice boundary of Australia, which is easily obtainable via the raster package.
library(tidyverse)library(raster)library(sf)aus <-getData("GADM", country ="AUS", level =0) %>%disaggregate() %>%geometry()
#> Warning in getData("GADM", country = "AUS", level = 0): getData will be removed in a future version of raster
#> . Please use the geodata package instead
#> Warning in explodePolygons(x, ...): No rgeos support in sp from October 2023;
#> see https://r-spatial.org/r/2023/05/15/evolution4.html
ggplot() +geom_sf(data =st_as_sf(aus))
To convert this map into hexagonal coordinates, we can use the spsample function to sample a hexagonal lattice. Through experimentation, a cellsize of 2 is roughly appropriate for the number of hexagons submitted to us. As it is a random process, some repetition may be needed to get the exact number of hexagons on the map with a nice layout.
hex_points <- aus %>%spsample(type ="hexagonal", cellsize =2)as_tibble(hex_points@coords)
#> # A tibble: 201 x 2
#> x y
#> <dbl> <dbl>
#> 1 146. -43.3
#> 2 145. -41.6
#> 3 147. -41.6
#> 4 141. -38.1
#> 5 143. -38.1
#> 6 145. -38.1
#> 7 147. -38.1
#> 8 140. -36.4
#> 9 142. -36.4
#> 10 144. -36.4
#> # i 191 more rows
To ensure that we are happy with the hexagon placements, we should view the hexagon grid on a map. We can convert our coordinates to hexagonal polygons using HexPoints2SpatialPolygons, and then plot them on the map.