knitr::opts_chunk$set(echo = TRUE, eval = TRUE)
library(knitr)
set.seed(12314159)

$\renewcommand{\tr}[1]{{#1}^{\mkern-1.5mu\mathsf{T}}}$ $\renewcommand{\ve}[1]{\mathbf{#1}}$ $\renewcommand{\sv}[1]{\boldsymbol{#1}}$ $\renewcommand{\pop}[1]{\mathcal{#1}}$ $\renewcommand{\samp}[1]{\mathcal{#1}}$ $\renewcommand{\imply}{\Longrightarrow}$ $\renewcommand{\given}{~\vert~}$ $\renewcommand{\suchthat}{~:~}$ $\renewcommand{\widebar}[1]{\overline{#1}}$ $\renewcommand{\wig}[1]{\tilde{#1}}$ $\renewcommand{\bigwig}[1]{\widetilde{#1}}$ $\renewcommand{\field}[1]{\mathbb{#1}}$ $\renewcommand{\Reals}{\field{R}}$ $\renewcommand{\abs}[1]{\left\lvert ~{#1} ~\right\rvert}$ $\renewcommand{\size}[1]{\left\lvert {#1} \right\rvert}$ $\renewcommand{\tr}[1]{{#1}^{\mkern-1.5mu\mathsf{T}}}$ $\renewcommand{\norm}[1]{\left|\left|{#1}\right|\right|}$ $\renewcommand{\intersect}{\cap}$ $\renewcommand{\union}{\cup}$

In this vignette, we show how to use l_micromaps() to construct interactive micromaps in loon (as implemented in the package loon.micromaps).

library(loon.micromaps)

Canadian census data -- construction

Spatial data: Canadian census divisions

The spatial polygons for Canadian census divisions from south-western Ontario in 2006 are contained in

data("cd_SWOntario_2006")

and appear as Figure \ref{fig:fig1} when plotted:

```rSouth Western Ontario Census Divisions"} plot(cd_SWOntario_2006)

## Canadian demographics census data

The Canadian demographic data from the 2006 census for each census division.

```r
data("canada_demographics_2006")

which must be merged with the spatial data:

cd_SWOntario_2006@data <- merge(cd_SWOntario_2006@data, canada_demographics_2006,
                                all.x = T, all.y = F, by.y = 'id', by.x = 'CDUID')

names(cd_SWOntario_2006@data)

Linked micromaps

The following creates the linked micromaps display with percent immigrant population, population density and education rate for the south western Ontario census divisions, as well as the histogram of cube root of the population density. The data points are linked using census division names as the linkingKey. The result is shown in Figure \ref{fig:fig2}

Two inspectors accompany the display - the usual loon inspector and the custom inspector for linked micromaps, which allows for updating many aspects of the display.

mm <- l_micromaps(spdf = cd_SWOntario_2006,
                  lab.label = 'Census Divisions',
                  variables = list(id.var = 'CDNAME',
                                   grouping.var = 
                                     list(name = 'pct_immigrants',
                                          xlab = 'percent'), 
                                   var2 = list(name = 'pop_density', 
                                               xlab = 'Pop per sq. km'),
                                   var3 = list(name = 'bachelor_above', 
                                               xlab = 'percent')),
                  spacing = 'equal', n_groups = 5,
                  linkingGroup = 'SWOntario', 
                  linkingKey = cd_SWOntario_2006@data$CDNAME,
                  sync = 'push',
                  showItemLabels = TRUE, 
                  itemLabel = cd_SWOntario_2006@data$CDNAME)

l_hist(cd_SWOntario_2006@data$"pop_density_cuberoot", 
       xlabel = "Cube root of population density per square km",
       showScales = TRUE, showGuides = TRUE,
       yshows = "density",
       linkingGroup = 'SWOntario', 
       linkingKey = mm$linkingKey)

```rSouth Western Ontario Demographics, Linked Micromaps and Histogram", out.width = "100%"} knitr::include_graphics("figures/canada_micromaps.png")

The `l_micromaps` function returns the Tk toplevel window object, `linkingGroup` value, `linkingKey` values, and the handles for the loon plot objects corresponding to the legends panel, scatterplot panel(s) and map panel in list form. This makes it possible to modify the plot objects after the display is created. For example, the following updates the color of points in the first group (result in Figure \ref{fig:fig3}):

```r
l_configure(mm$scatterplots[[1]][1], color = 'red')

```rUpdated Linked Micromaps", out.width = "100%", fig.align = "center"} knitr::include_graphics("figures/canada_micromaps_configure.png")

The main interactive features of linked micromaps is the linkage of data points and certain plot attributes between various panels/columns. Figure \ref{fig:fig4} shows how to zoom, pan and select points using mouse and keyboard gestures.

```rMouse and Keyboard Gestures for Zoom, Pan and Selection", out.width = "100%", fig.align = "center"}
knitr::include_graphics("figures/gestures.png")

\vspace*{5cm}

References



x249wang/loon.micromaps documentation built on May 10, 2019, 8:30 a.m.