I saw a cool presentation of geographic information on the StatsChat blog http://www.statschat.org.nz/2014/10/06/nz-voting-cartograms/.

This is an updated hexagonal cartogram design of Chris McDowall, which is also similar to the style of presentation of the cartogram maps of Leicestershire County Council.

I wanted to figure out how to make this style of map easily in R, so after a bit of web searching for terms like “hex plot in R” and similar, came up with the hexbin package and hexagon from the plotix package. hexbin is more aimed at summary data so I decided to experiment with hexagon to see if it would do what I wanted. Note: the plot area is set to be square

x=1:5
y=1:5
plot(x,y, type="n")
library(plotrix)
hexagon(1,1, col="red")
hexagon(1,2, col="green")
hexagon(2,1, col="blue")
hexagon(2,2, col="yellow")

plot of chunk unnamed-chunk-1

Well, it did at least plot hexagons, though I didn’t quite get the coordinate system correct at first try, so let’s make a second try at it

x=1:5
y=1:5
plot(x,y, type="n")
library(plotrix)
hexagon(1,1, col="red")
hexagon(1.5,2, col="green")
hexagon(2,1, col="blue")
hexagon(2.5,2, col="yellow")

plot of chunk unnamed-chunk-2

That is the coordinate system understood, so now lets tweak the plot settings a little.

x=1:5
y=1:5
plot(x,y, type="n", frame.plot=F, xaxt="n", yaxt="n", xlab="", ylab="")
library(plotrix)
hexagon(1,1, col="red", unitcell=0.95,border="white")
hexagon(1.5,2, col="green", unitcell=0.95,border="white")
hexagon(2,1, col="blue", unitcell=0.95,border="white")
hexagon(2.5,2, col="yellow", unitcell=0.95,border="white")

plot of chunk unnamed-chunk-3

Now let’s borrow David Figgin’s updated 2014 coordinate arrangement of Chris McDowall’s original. Let’s also startputting the plotting into a function

library(plotrix)
hexmap <- function(xcor,ycor,colval){
  plot(min(c(xcor,ycor)):(max(c(xcor,ycor))+1),min(c(xcor,ycor)):(max(c(xcor,ycor))+1), type="n", frame.plot=F, xaxt="n", yaxt="n", xlab="", ylab="")
  data <- data.frame(xcor,ycor,colval)
  apply(data, 1, function(zone) hexagon(zone[1],zone[2],col=zone[3], unitcell=0.95,border="white"))
}

data <- read.table(text="
                   Where x y colour
                   Invercargill 1 1 1
                   Clutha 1.5 2 2
                   DunedinSouth 2 3 3
                   DunedinNorth 3 3 4
                   Waitaki 2.5 4 5
                   ", header=TRUE)

hexmap(data$x,data$y, data$colour)

plot of chunk unnamed-chunk-4

## NULL

Other enhancements are making labels on the graph with text() and making colour gradients to show the strength of values, but this seems to be the basic plan.

library(plotrix)
hexmap <- function(xcor,ycor,colval, label){
  colval <- as.integer(colval/max(colval) * 180)
  plot(min(c(xcor,ycor)):(max(c(xcor,ycor))+1),min(c(xcor,ycor)):(max(c(xcor,ycor))+1), type="n", frame.plot=F, xaxt="n", yaxt="n", xlab="", ylab="")
  data <- data.frame(xcor,ycor,colval)
  apply(data, 1, function(zone) hexagon(zone[1],zone[2],col=hcl(zone[3]), unitcell=0.95,border="white"))
  text(xcor+ 0.5,ycor + 0.5,labels=label, cex=0.7)
}

data <- read.table(text="
                   where x y colour
                   Invercargill 1 1 1
                   Clutha 1.5 2 2
                   DunedinSouth 2 3 3
                   DunedinNorth 3 3 4
                   Waitaki 2.5 4 5
                   ", header=TRUE)

hexmap(data$x,data$y, data$colour, data$where)

plot of chunk unnamed-chunk-5

this should be giving people enough to be going on with for making their own versions of this style of map/ plot/ quasimap/ cartogram.