2

I would like to create a choropleth map visualising 2 dimensional values (= color fill the polygons according to an ordered pair (v1, v2), where v1 and v2 are ordered factors).

Here is an example of how the result should look like: enter image description here

I think the color matrix legend with the two dimensions makes it clear what I want to achieve.

I would like to implement this using ggplot2::geom_polygon.


Minimal example:

ids <- factor(c("1.1", "2.1", "1.2", "2.2", "1.3", "2.3"))

values <- data.frame(
    id = ids,
    v1 = factor(c("Hi","Med","Med","Hi","Lo","Lo"), 
                levels=c("Lo", "Med", "Hi"), ordered=TRUE),
    v2 = factor(c("Hi","Lo","Lo","Med","Med","Hi"), 
                levels=c("Lo", "Med", "Hi"), ordered=TRUE))

positions <- data.frame(
  id = rep(ids, each = 4),
  x = c(2, 1, 1.1, 2.2, 1, 0, 0.3, 1.1, 2.2, 1.1, 1.2, 2.5, 1.1, 0.3,
        0.5, 1.2, 2.5, 1.2, 1.3, 2.7, 1.2, 0.5, 0.6, 1.3),
  y = c(-0.5, 0, 1, 0.5, 0, 0.5, 1.5, 1, 0.5, 1, 2.1, 1.7, 1, 1.5,
        2.2, 2.1, 1.7, 2.1, 3.2, 2.8, 2.1, 2.2, 3.3, 3.2))

datapoly <- merge(values, positions, by=c("id"))

I would like to combine the two following maps in just one following the example above. The color fill of the polgons should be according to the ordered pair (v1, v2) and of course I would need a color matrix legend.

library("ggplot2")
ggplot(datapoly, aes(x=x, y=y)) + geom_polygon(aes(fill=v1, group=id))
ggplot(datapoly, aes(x=x, y=y)) + geom_polygon(aes(fill=v2, group=id))

enter image description here

chamaoskurumi
  • 2,271
  • 2
  • 23
  • 30
  • I asked a similar question a while ago - focused purely on the 2-dimensional legend idea: http://stackoverflow.com/q/20129299/903061 – Gregor Thomas May 11 '16 at 20:47

2 Answers2

1

Bit late to the party here, but, for the benefit of anyone coming across this question later, https://github.com/wmurphyrd/colorplaner seems to do exactly what you want. The second example in the usage section even gives an example of coloring in a map.

user3137493
  • 350
  • 1
  • 8
-1

How about interaction(v1, v2) and map a single scale on the resulting interaction?

# using your inputs
values$v1_2 <- interaction(values$v1, values$v2)
...
datapoly <- merge(values, positions, by=c("id"))

library(ggplot2)
library(scales)
library(RColorBrewer)
ggplot(datapoly, aes(x=x, y=y)) + geom_polygon(aes(fill=v1_2, group=id)) +
  scale_fill_brewer(palette= "Blues")

enter image description here

Or use the solution @gregor linked to in the comments above.

alexwhitworth
  • 4,839
  • 5
  • 32
  • 59
  • mh, i really would like to have a matrix legend and not a single scale...The link @gregor posted above definately goes into the right direction... – chamaoskurumi May 11 '16 at 21:11