# NOT RUN {
map <- checkerboard(2)
cartogram <- recmap(map)
map
cartogram
op <- par(mfrow = c(1, 2))
plot(map)
plot(cartogram)
## US example
usa <- 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)
usa$z <- state.x77[, 'Population']
US.Map <- usa[match(usa$name,
c('Hawaii', 'Alaska'), nomatch = 0) == 0, ]
plot.recmap(US.Map)
plot(recmap(US.Map))
par(op)
# define a fitness function
recmap.fitness <- function(idxOrder, Map, ...){
Cartogram <- recmap(Map[idxOrder, ])
# a map region could not be placed;
# accept only feasible solutions!
if (sum(Cartogram$topology.error == 100) > 0){return (0)}
1 / sum(Cartogram$z / (sqrt(sum(Cartogram$z^2)))
* Cartogram$relpos.error)
}
# }
# NOT RUN {
## use Genetic Algorithms (GA >=3.0.0) as metaheuristic
M <- US.Map
recmapGA <- ga(type = "permutation",
fitness = recmap.fitness,
Map = M,
monitor = gaMonitor,
min = 1, max = nrow(M) ,
popSize = 4 * nrow(M),
maxiter = 10,
run = 100,
parallel=TRUE,
pmutation = 0.25)
plot(recmap(M[recmapGA@solution[1,],]))
plot(recmapGA)
# }
# NOT RUN {
# }
# NOT RUN {
# install.packages('rbenchmark')
library(rbenchmark)
benchmark(recmap(US.Map[sample(50,50),]), replications=100)
## test replications elapsed relative user.self
##1 recmap(US.Map[sample(50, 50), ]) 100 1.255 1 1.124
## sys.self user.child sys.child
##1 0.038 0 0
# }
# NOT RUN {
## Have Fun!
# }
Run the code above in your browser using DataLab