In advance of the recent UK general election, ODI Leeds published an interactive hexmap of constituencies to provide a navigation surface over various datasets relating to Westminster constituencies:
As well as the interactive front end, ODI Leeds published a simple JSON format for sharing the hex data – hexjson that allows you to specify an identifier for each, some data values associated with it, and relative row (
r) and column (
It’s not hard to imagine the publication of default, or base, hexJSON documents that include standard identifier codes and appropriate co-ordinates, e.g. for Westminster constituencies, wards, local authorities, and so on being developed around such a standard.
So that’s one thing the standard affords – a common way of representing lots of different datasets.
Tooling can then be developed to inject particular data-values into an appropriate
hexJSON file. For example, a
hexJSON representation of UK HEIs could add a data attribute identifying whether an HEI received a Gold, Silver or Bronze TEF rating. That’s a second thing the availability of a standard supports.
By building a UI that reads data in from a hexJSON file, ODI Leeds have developed an application that can presumably render other people’s hexJSON files, again, another benefit of a standard representation.
But the availability of the standard also means other people can build other visualisation tools around the standard. Which is exactly what Oli Hawkins did with his
You can see an example here, created by Henry Lau:
You maybe start to get a feel for how this works… Data in a standard form, standard library that renders the data. For example, Giuseppe Sollazzo (aka @puntofisso), had a play looking at voter swing:
So… one of the things I was wondering was how easy it would be for folk in the House of Commons Library, for example, to make use of the
Step in HTMLwidgets, a (standardised) format for publishing interactive HTML widgets from Rmarkdown (Rmd). The idea is that you should be able to say something like:
hexjsonwidget( hexjson )
and embed a rendering of a
d3-hexjson map in HTML output from a knitred Rmd document.
hexjson as a JSON file from a base (hexJSON) file with custom data values added to it is the next step, and the next thing on my to do list.)
It’s little more than a wrapping of the demo template, and I’ve only tested it with a single example hexJSON file, but it does generate d3.js hexmaps:
library(jsonlite) library(hexjsonwidget) jj=fromJSON('./example.hexjson') hexjsonwidget(jj)
It also needs documenting. And support for creating data-populated base hexJSON files. But it’s a start. And another thing the hexJSON has unintentionally provided supported for.
But it does let you create HTML documents with embedded hexmaps if you have the hexJSON file handy:
By the by, it’s also worth noting that we can also publish an image snapshot of the SVG hexjson map in a
knitr rendering of the document to a PDF or Microsoft Word output document format:
At first I thought this wasn’t possible, and via @timelyportfolio found a workaround to generate an image from the SVG:
library(exportwidget) library(webshot) library(htmltools) library(magrittr) html_print(tagList( hexjsonwidget(jj) , export_widget( ) ), viewer=NULL) %>% webshot( delay = 3 )
But then noticed that the PDF rendering was suddenly working – it seems that if you have the
htmltools packages installed, then the PDF and Word rendering of the HTMLwidget SVG as an image works automagically. (I’m not sure I’ve seen that documented – the related HTMLwidget Github issue still looks to be open?)
See also: HexJSON HTMLWidget for R, Part 2, in which support for custom hex colour and labeling is added, and HexJSON HTMLWidget for R, Part 3, where I add in the ability to merge an R dataframe into a hexjson object, and create a hexjsonwidget directly from a dataframe.