This package contains a C++ implementation of the RecMap algorithm [1] to draw maps according to given statistival values. These so-called cartograms or value-by-area-maps may be used to visualize any geospatial-related data, e. g. political, economical or public health data. The input consists of a map represented by overlapping rectangles. This map is defined by the following parameters for each map region:
The (x, y) coordinates represent the center of the minimal bounding boxes (MBB), The coordinates of the MBB are derived by adding or substracting the (dx, dy) tuple accordingly. The tuple (dx, dy) defines also the ratio of the map region. The statistical values define the desired area of each map region.
The output is a rectangular cartogram where the map regions are:
The construction heuristic places the rectangles in a way that important spatial constraints, in particular
are tried to be preserved.
The ratios are preserved and the area of each region corresponds to the as input given statistical value z.
The power of a cartogram based visualization is demonstrated below.
Rectangular Cartogram of the U.S. election 2004; The area corresponds to the number of electors (color indicates the party red: democrates / blue: republican; the color intensity ~ outcome of the vote.). The graphic was computed by using the original implementation of the construction heuristic RecMap MP2 introduced in [1].
state
Facts and Figures Datasetusa <- data.frame(x=state.center$x,
y = state.center$y,
# make the rectangles overlapping by correcting lines of longitude distance
dx = sqrt(state.area) / 2 / (0.8 * 60 * cos(state.center$y*pi/180)),
dy = sqrt(state.area) / 2 / (0.8 * 60) ,
z = sqrt(state.area),
name = state.name)
The desired area \(\tilde{A_j}\) of a map region \(r_j\) is defined as \[\tilde{A_j}= z_j \cdot \frac {\sum_{i=1}^{k}A(r_j)} {\sum_{j=1}^{k} z_{j}}\] (CTRL-C CTRL-V from [2]).
The rectangles have to overlap to compute the dual graph. This enables to generate valid input having only the (x, y) coordinates of the map region.
op<-par(mfrow=c(1,1), mar=c(0,0,0,0))
library(recmap)
plot_recmap(M <- usa[!usa$name %in% c("Hawaii", "Alaska"), ], col.text = 'black', lwd=2)
A local placement function can place an rectangle a around a given rectangle b of any angle between \([-\pi,\pi]\). There are special cases for quad I, II, III, and IV indicated by different colors in the gaphic below.
The index order of the input map has an impact to the resulting cartogram. This is caused due do the computation of the dual graph. In [1] a genetic algorithm was applied as meta heuristic. Due do the limited compute resources on the CRAN package build process no meta heuristic will be applied in this R package.
The topology error is an indicator of the deviation of the neighborhood relationships. The error is computed by counting the differences beween dual graphs or adjacency graphs of map and cartogram (for a more formal definition see [1]).
Measures the angle difference between alles region centers.
The empty space is the ratio between the areas of the regions and the area of the global minimal bounding box.
The output is again a data.frame
.
head(Cartogram <- recmap(Map <- usa[!usa$name %in% c("Hawaii", "Alaska"), ]))
## x y dx dy z name dfs.num
## 1 -86.12445 24.40204 3.201992 2.697824 227.17614 Alabama 31
## 2 -123.13242 29.61981 3.939586 3.257613 337.50407 Arizona 38
## 3 -85.30009 39.65352 3.265364 2.683509 230.44305 Arkansas 33
## 4 -131.42392 31.53933 4.341915 3.488740 398.36290 California 37
## 5 -122.00152 23.21422 3.965671 3.095895 322.87304 Colorado 47
## 6 -53.13517 41.31657 1.896954 1.418697 70.77429 Connecticut 19
## topology.error relpos.error
## 1 0 0.2287945
## 2 0 0.2110718
## 3 0 0.7596130
## 4 0 0.1369841
## 5 0 0.6025215
## 6 0 0.1954060
state.x77[, 'Population']
op<-par(mfrow=c(1,1), mar=c(0,0,0,0))
usa$z <- state.x77[, 'Population']
M <- usa[!usa$name %in% c("Hawaii", "Alaska"), ]
plot_recmap(Cartogram.Population <- recmap(M[order(M$x),]), col.text = 'black', lwd=2)
Area ~ population estimate as of July 1, 1975;
op<-par(mfrow=c(1,1), mar=c(0,0,0,0))
# index order
smp <- c(20,47,4,40,9,6,32,33,3,10,34,22,2,28,15,12,39,7,42,45,19,13,43,30,24,
25,11,17,37,41,26,29,21,35,8,36,14,16,31,48,46,38,23,18,1,5,44,27)
plot_recmap(Cartogram.Population <- recmap(M[smp,]), col.text = 'black', lwd=2)
Area ~ population estimate as of July 1, 1975; a better index order has been choosen to minimize the relative position error.
state.x77[, 'Income']
op<-par(mfrow=c(1,1), mar=c(0,0,0,0))
usa$z <- state.x77[, 'Income']
M <- usa[!usa$name %in% c("Hawaii", "Alaska"), ]
plot_recmap(Cartogram.Income <- recmap(M[order(M$x),]), col.text = 'black', lwd=2)
Area ~ capita income (1974);
state.x77[, 'Frost']
op<-par(mfrow=c(1,1), mar=c(0,0,0,0))
usa$z <- state.x77[, 'Frost']
M <- usa[!usa$name %in% c("Hawaii", "Alaska"), ]
plot_recmap(Cartogram.Income <- recmap(M[order(M$x),]), col.text = 'black', lwd=2)
Area ~ mean number of days with minimum temperature below freezing (1931–1960) in capital or large city;
Checker boards provide examples of sets of map regions which do not have ideal cartogram solutions according to Definition 1 [2].
op<-par(mar=c(0,0,0,0), mfrow=c(1, 2), bg='white')
plot_recmap(ChessBoard <- recmap:::.checker_board(8),
col=c('white','white','white','black')[ChessBoard$z])
# found by a Non-deterministic Turing machine
smp <- c(8,56,18,5,13,57,3,37,62,58,7,16,40,59,17,34,29,41,46,27,54,43,2,21,
38,52,31,20,28,48,1,22,55,11,25,19,50,10,24,53,47,30,45,44,32,35,51,
15,64,12,14,39,26,6,42,33,4,36,63,49,60,61,9,23)
plot_recmap(Cartogram.ChessBoard <- recmap(ChessBoard[smp,]),
col=c('white','white','white','black')[Cartogram.ChessBoard$z])
chess board fun
The work on RecMap was initiated by understanding the limits of contiguous cartogram drawing [2] and after studying the visualizations drawn by Erwin Raisz [3]. The purpose of the first implementation [1] was a feasibility check on how computer-generated rectangular cartograms could look like. This R package provides a rectangular cartogram algorithm to be used by any R user. Now, it is easy to generate input (e.g, no complex polygon mesh), the code is maintainable, and the algorithm is made robust to the price of not having all featues implemented. Recent research publication on rectangular cartograms drawing include [4], [5], [6].
[1] R. Heilmann, D. A. Keim, C. Panse, and M. Sips, “RecMap: Rectangular Map Approximations,” in 10th IEEE Symposium on Information Visualization (InfoVis 2004), 10-12 October 2004, Austin, TX, USA, 2004, pp. 33–40.
[2] D. A. Keim, S. C. North, and C. Panse, “CartoDraw: A Fast Algorithm for Generating Contiguous Cartograms,” IEEE Trans. Vis. Comput. Graph., vol. 10, no. 1, pp. 95–110, 2004.
[3] E. Raisz, “The Rectangular Statistical Cartogram,” Geographical Review., vol. 24, no. 2, pp. 292–296, 1934.
[4] M. J. van Kreveld and B. Speckmann, “On rectangular cartograms,” in Algorithms - ESA 2004, 12th annual european symposium, bergen, norway, september 14-17, 2004, proceedings, 2004, pp. 724–735.
[5] M. J. van Kreveld and B. Speckmann, “On rectangular cartograms,” Comput. Geom., vol. 37, no. 3, pp. 175–187, 2007.
[6] K. Buchin, B. Speckmann, and S. Verdonschot, “Evolution strategies for optimizing rectangular cartograms,” in Geographic information science - 7th international conference, gIScience 2012, columbus, oH, uSA, september 18-21, 2012. proceedings, 2012, pp. 29–42.