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:
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))