Skip to content

Birmingham-and-Solihull-ICS/hexmap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hexmap

Hex maps for ICS using Simple Features

A markdown file and necessary csv files to build hexmpas of ICS.

A bit of a dump I am afraid - need to tidy it up.

What is the purpose of this blog?

I am going to introduce how to use Simple Features to look at creating bespoke maps using co-ordinate system grids.

What is the problem?

It all started with trying to do a map of ICS to show variation but being flummoxed by the different size of each of them. London is always swamped or has to be mapped seperately, and North East always draws the eye as it is so big.

ICS regional map

So what can we do? Hexmapping

One approached used to address such variation is to use a hex map. Hex maps have become the standard when visualizing data where the sizing of the geographical region is unimportant. It is not new, here is an example from 1895, The Unification of London: The Need and the Remedy. (https://mappinglondon.co.uk/2013/hexagonal-map-of-london/)

Here is an example using UK constituencies https://open-innovations.org/projects/hexmaps/constituencies/. As you can see visual wieght of Scotland has shrunk whist the more populous London takes up more space, but the map retains traces of the orignal geography, unlike a bar chart. Or visit this dynamic example of regular to hex geography by ONS https://www.ons.gov.uk/visualisations/nesscontent/dvc237/hex.html.

UK constituency hex map

Why hexagons?

Hexagons are a good shape to work with as the distances between its points are regular due to its internal symmetry.

There are two orientations you can use pointed or flat tops. Basically rotate the hexagon by 90degrees. Pointed-top hexes take up more vertical room, so can be better for portrait orientation.The choice is yours depending on your aesthetics. You can learn more about hexagon geomtery at https://hexnet.org/content/hexagonal-geometry.

Heaxgon geometry

Mapping the ICS geography

There are no tools to build hex maps from shapefiles. So you have to get out paper and pencil/ipad/hex stickers and a wall to map out the relative positions of the objects you want to map. I used a hex paper image and ipad. One free resource for printing hex paper is https://incompetech.com/graphpaper/hexagonal/ .

There is no science to this, it really is what works best for the geography you are working with. This is an example of my rough working where to put STP/ICS.

Heaxgon geometry

The co-ordinates are kept quite simple by setting the width and height to be 4 units. Then using the row and column positions (starting from the origin - bottom left) to build the co-ordinates. So in my mapping Cornwall is in row 1 and column 1, and so on.

The end result of all this plotting is a table of with a row for every hex and 7 not 6 points, you have to complete the shape.

Heaxgon geometry


library(tidyverse) #tidyverse
library(knitr) #tidyverse

library(kableExtra) #tidyverse

#https://www.england.nhs.uk/integratedcare/integrated-care-in-your-area/
ics<-read_csv("ics_coords2.csv")


ics$x1=ics$col
ics$x2=ics$x1
ics$x3=ics$x2+2
ics$x4=ics$x3+2
ics$x5=ics$x4
ics$x6=ics$x5-2
ics$x7=ics$x1

ics$y1=ics$row*3-2
ics$y2=ics$y1+2
ics$y3=ics$y2+1
ics$y4=ics$y2
ics$y5=ics$y1
ics$y6=ics$y5-1
ics$y7=ics$y1


ics[1:5,c(1:4,12:25)]%>%arrange(row,col)%>%kbl(caption="ICS geometery")%>%
  kable_styling()

Table

SF workflow

https://r-spatial.github.io/sf/index.html

The key to creating polygons is that the coordinates have to be in sequence to form a closed area (i.e., the last point is the same as the first point).

So your data will need a bit of manipulation to create the coordinates, and put them in order. I ran into difficulties with this and used this solution. (https://stackoverflow.com/questions/63566637/rror-in-funxi-is-numericx-is-not-true-when-using-st-polygon)

There are three steps to creating a SF

  • st_polygon() - Create simple feature from a numeric vector, matrix or list

  • st_sfc() - Create simple feature geometry list column

  • st_sf() - Create sf object

Merge a Spatial object having a data.frame (i.e. merging of non-spatial attributes).

sp::merge is located in package sp. Please install and load package sp before us


library(sf)  #simple features
library(sp)  #simple features

#https://stackoverflow.com/questions/63566637/rror-in-funxi-is-numericx-is-not-true-when-using-st-polygon
class(ics) <- 'data.frame'

icsdata<-read_csv("ics_data.csv")

#https://stackoverflow.com/questions/44335246/polygons-from-coordinates
lst <- lapply(1:nrow(ics), function(x){
  ## create a matrix of coordinates that also 'close' the polygon
  res <- matrix(c(ics[x, 'x1'], ics[x, 'y1'],
                  ics[x, 'x2'], ics[x, 'y2'],
                  ics[x, 'x3'], ics[x, 'y3'],
                  ics[x, 'x4'], ics[x, 'y4'],
                  ics[x, 'x5'], ics[x, 'y5'],
                  ics[x, 'x6'], ics[x, 'y6'],
                  ics[x, 'x7'], ics[x, 'y7'])  ## need to close the polygon
                , ncol =2, byrow = T
  )
  ## create polygon objects
  st_polygon(list(res))
  
})

## st_sfc : creates simple features collection
## st_sf : creates simple feature object
sfdf <- st_sf(ICS22NM= ics[, 'ICS22NM'], st_sfc(lst))

#merge SF object to ics data frame and the centroids of the hexagon for the labels

ics_map<-sp::merge(sfdf,ics) %>% 
  mutate(labelX = map_dbl(geometry, ~st_centroid(.x)[[1]]),
         labelY = map_dbl(geometry, ~st_centroid(.x)[[2]]))

icsdata<-icsdata%>%mutate(value=round(value,1))

Plotting the map

ggplot2() contains a geom for simple feature objects, geom_sf_data().

There is a geom_sf_label, but I think geom_text() is better.

#Join the data we want to map 
ics_map<-ics_map%>%left_join(icsdata)

ggplot() +
   geom_sf(data=ics_map,aes(fill=NHSER21NMSHT))+
  geom_sf_label(data=ics_map,aes(label = NHSER21NMSHT),
  label.padding = unit(0.1, "lines"))+
  labs(fill = "Region")+
    scale_fill_manual(values = c("NW" = "#ed8b00",
                                "NEY"="#0072ce",
                                "Mid"="#78be20",
                                "EoE"="#41b6e6",
                                "SW"="#ae2573",
                                "SE"="#330072",
                                "LDN"="#ffb81c"))+
  theme_void()

First attempt

Using geom_text(), I think it looks better, and the final map looks similar to the starting position

#using geom_text

icsplot<-ggplot() +
   geom_sf(data=ics_map,aes(fill=NHSER21NMSHT))+
geom_text(data = ics_map,aes(x = labelX, y = labelY, label = NHSER21NMSHT), colour="white")+
  labs(fill = "Region")+
    scale_fill_manual(values = c("NW" = "#ed8b00",
                                "NEY"="#0072ce",
                                "Mid"="#78be20",
                                "EoE"="#41b6e6",
                                "SW"="#ae2573",
                                "SE"="#330072",
                                "LDN"="#ffb81c"))+
  theme_void()

Hex geography Actual Geography

Plot a value

As we have merged the geospatial data with our ICS data, it is easy to then display this. Here I have used ggiraph to add an interactive element to the map. To do this you replace geom_sf() with gemo_sf_interactive(), and display the chart as htmlwidget using girafe().


library(ggiraph)      #interactive charts

tooltip_css <- "font:Arial;"

gg_point=ggplot() +  
  geom_sf_interactive(data = ics_map,aes( tooltip = value, data_id = ICS22NM, fill = value)) + 
scale_fill_viridis_c(option = "F", trans = "sqrt")+ 
  geom_text(data = ics_map,aes(x = labelX, y = labelY, label = ICS22NMSHT), colour="white")+
  theme_void()

girafe(ggobj = gg_point, 
  options = list(opts_tooltip(use_fill = TRUE),
                 opts_sizing(width = .7)))

Plotting data

About

Hex maps for ICS using Simple Features

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages