cranvas (version 0.8.5)

qmap: Draw a map

Description

This function creates a map based on the data of polygon coordinates. Cartograms can be made by providing appropriate coordinates generated by cart_polygon.

Usage

qmap(data, linkto = NULL, linkby = NULL, main = "", xlim = NULL, ylim = NULL, unibrushcolor = TRUE, googleMap = FALSE, place = NULL, path = NULL, text = NULL, cartostep = 0.05, ...)

Arguments

data
the map data created by map_qdata
linkto
a mutaframe to link to so that the colors and the brushed status are in sync with this data
linkby
the variable in the linkto data to be used as a linking variable (see link_cat)
unibrushcolor
whether to use multiple colors for the brush, i.e., whether to transparentize the unbrushed polygons.
googleMap
whether to have a google map background. The packages ggmap and RgoogleMaps must be installed.
place
a mutaframe created by qdata indicating the locations that will be shown on the map.
path
a mutaframe of locations created by qdata. A path will be drawn on the map in the order of the mutaframe.
text
a mutaframe of locations and names.
cartostep
a value between 0 and 1. When a cartogram is drawn, the left arrow and right arrow can be used to evolve the map between the original map and the cartogram. cartostep controls the evolving speed.
main
the main title
xlim
a numeric vector of length 2 (like c(x0, x1)) for x-axis limits; it will be calculated from the data limits if not specified (NULL). Note when x0 > x1, the axis direction will be reversed (i.e. from larger values to small values)
ylim
y-axis limits; similar to xlim

Value

A map

Details

Common interactions are documented in common_key_press. Currently the brushing may be slightly different with other plots: when a region contains multiple sub-regions (polygons), and the whole region will be brushed if any of the sub-regions is brushed.

The other feature is that a map can be linked to other datasets so that colors and the brushed status can be obtained from an additional data. This is useful due to the special format of map data: usually it is a waste of memory to store colors for every single boundary points in each region.

Examples

Run this code
library(cranvas)

### (1) simple maps the world map
qworld <- map_qdata("world")
qmap(qworld)

## US states
qstate <- map_qdata("state")
qmap(qstate)

# get a google map background The packages ggmap and
# RgoogleMaps must be installed.  Warning: ggmap also has a
# function named qmap.
qmap(qstate, googleMap = TRUE)

# a simple demonstration of colors
qstate$.color <- sample(c("red", "blue"), nrow(qstate), replace = TRUE)

# Draw a path on the map
MyTrip <- data.frame(x = c(-80.134, -84.39, -93.62, -95.383, -122.333, 
    -117.1625), y = c(25.81, 33.755, 42.0347, 29.763, 47.6097, 
    32.715), place = c("Miami", "Atlanta", "Ames", "Houston", 
    "Seattle", "San Diego"))
qMyTrip <- qdata(MyTrip, color = "black", size = c(1, 0.5, 3, 
    1, 2, 1))
qmap(qstate, place = qMyTrip, path = qMyTrip, text = qMyTrip, 
    halign = "center", valign = "bottom", cex = 1.25)

## US counties
qcounty <- map_qdata("county")
qmap(qcounty)

## map of China
if (require("mapdata")) {
    qchina <- map_qdata("china")
    qmap(qchina)  # oh my! obviously china map needs more love
}



### (2) maps linked to other datasets
head(crimes)
qcrimes <- qdata(crimes, color = population)  # population mapped to colors
qcrimes <- qdata(crimes, color = population, low = "#FCBBA1", 
    high = "red")  # change the color scheme from http://colorbrewer2.org/

qparallel(names(crimes)[-c(1, 2)], data = qcrimes)  # par-coords without first 2 columns
qscatter(population, robbery, data = qcrimes)

## categorical linking by states; now brush on any plot and see
## changes in other plots
qmap(qstate, linkto = qcrimes, linkby = "state")
qmap(qstate, linkto = qcrimes, linkby = "state", unibrushcolor = FALSE)

## linking should be automatically removed when map is closed;
## if not, try remove_link(qcrimes); remove_link(qstate)



### (3) cartogram (presidential election results)

vote.res <- c("red", "red", "red", "blue", "blue", "blue", "blue", 
    "blue", "blue", "red", "red", "blue", "blue", "blue", "red", 
    "red", "red", "blue", "blue", "blue", "blue", "blue", "blue", 
    "blue", "blue", "red", "red", "red", "red", "blue", "blue", 
    "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", 
    "blue", "red", "blue", "red", "blue", "blue", "blue", "red", 
    "red", "red", "red", "red", "blue", "blue", "blue", "blue", 
    "blue", "blue", "blue", "blue", "blue", "red", "blue", "red")

qstate <- map_qdata("state", color = vote.res)
qmap(qstate)  # a normal map

## cartogram based on population

vote.pop <- c(4708708, 6595778, 2889450, 36961664, 5024748, 3518288, 
    885122, 601723, 18537969, 9829211, 1545801, 12910409, 6423113, 
    3007856, 2818747, 4314113, 4492076, 1318301, 5699478, 6593587, 
    6593587, 6593587, 9969727, 9969727, 5266214, 2951996, 5987580, 
    974989, 1796619, 2643085, 1324575, 8707739, 2009671, 19541453, 
    19541453, 19541453, 19541453, 9380884, 9380884, 9380884, 646844, 
    11542645, 3687050, 3825657, 12604767, 1053209, 4561242, 812383, 
    6296254, 24782302, 2784572, 621760, 7882590, 7882590, 7882590, 
    6664195, 6664195, 6664195, 6664195, 6664195, 1819777, 5654774, 
    544270)

qstate2 <- map_qdata("state", color = vote.res, size = vote.pop, 
    cartogram = TRUE, diffuse = 2)
qmap(qstate2)  # try to press left arrow and right arrow

## electoral votes for each state
vote.num <- c(9, 10, 6, 55, 9, 7, 3, 3, 27, 15, 4, 21, 11, 7, 
    6, 8, 9, 2, 10, 12, 12, 12, 17, 17, 10, 6, 11, 3, 2, 5, 4, 
    15, 5, 31, 31, 31, 31, 15, 15, 15, 3, 20, 7, 7, 21, 4, 8, 
    3, 11, 34, 5, 3, 13, 13, 13, 11, 11, 11, 11, 11, 5, 10, 3)

qstate3 <- map_qdata("state", color = vote.res, size = vote.num, 
    cartogram = TRUE)
qmap(qstate3)

cranvas_off()

Run the code above in your browser using DataLab