View source: R/plot_functions.R
pop.map | R Documentation |
Generates a world map of various population measures for a given quantile and a projection or observed period, using different techniques: pop.map
use rworldmap, pop.ggmap
uses ggplot2, and pop.map.gvis
creates an interactive map via GoogleVis.
pop.map(pred, sex = c("both", "male", "female"), age = "all", expression = NULL, ...)
pop.ggmap(pred, sex=c('both', 'male', 'female'), age='all', expression=NULL, ...)
get.pop.map.parameters(pred, expression = NULL, sex = c("both", "male", "female"),
age = "all", range = NULL, nr.cats = 50, same.scale = TRUE, quantile = 0.5, ...)
pop.map.gvis(pred, ...)
pred |
Object of class |
sex |
One of “both” (default), “male” or “female”. By default the male and female counts are summed up. This argument is only used if |
age |
Either a character string “all” (default) or an integer vector of age indices. Value 1 corresponds to age 0-4, value 2 corresponds to age 5-9 etc. Last age goup |
expression |
Expression defining the population measure to be plotted. For syntax see |
range |
Range of the population measure to be displayed. It is of the form |
nr.cats |
Number of color categories. |
same.scale |
Logical controlling if maps for all years of this prediction object should be on the same color scale. |
quantile |
Quantile for which the map should be generated. It must be equal to one of the values in |
... |
Additional arguments passed to the underlying functions. In |
pop.map
creates a single map for the given time period and quantile. If the package fields is installed, a color bar legend at the botom of the map is created.
Function get.pop.map.parameters
can be used in combination with pop.map
. It sets breakpoints for the color scheme.
Function pop.ggmap
is similar to pop.map
, but uses the ggplot2 package in combination with the geom_sf
function.
Function pop.map.gvis
creates an interactive map using the googleVis package and opens it in an internet browser. It also generates a table of the mapped values that can be sorted by columns interactively in the browser.
get.pop.map.parameters
returns a list with elements:
pred |
The object of class |
quantile |
Value of the argument |
catMethod |
If the argument |
numCats |
Number of categories. |
coulourPalette |
Subset of the rainbow palette, starting from dark blue and ending at red. |
... |
Additional arguments passed to the function. |
If the expression
argument or a non-standard combination of sex and age is used, quantiles are computed on the fly. In such a case, trajectory files for all countries have to be loaded from disk, which can be quite time expensive. Therefore a simple caching mechanism was added to the prediction object which allows re-using data from previously used expressions. The prediction object points to an environment called cache
which is a collection of data arrays that are results of evaluating expressions. The space-trimmed expressions are the names of the cache
entries. Every time a map function is called, it is checked if the corresponding expression is contained in the cache
. If it is not the case, the quantiles are computed on the fly, otherwise the existing values are taken.
When computing on the fly, the function tries to process it in parallel if possible, using the package parallel. In such a case, the computation is split into n
nodes where n
is either the number of cores detected automatically (default), or the value of getOption("cl.cores")
. Use options(cl.cores=n)
to modify the default. If a sequential processing is desired, set cl.cores
to 1.
The cache data are also stored on disk, namely in the simulation directory of the prediction object. By default, every update of the cache in memory is also updated on the disk. Thus, data expression results can be re-used in multiple R sessions. Function pop.cleanup.cache
deletes the content of the cache. This behaviour can be turned off by setting the argument write.to.cache=FALSE
in the get.pop.prediction
function. We use this settings in the examples throughout this manual whenever the example data from the installation directory is used, in order to prevent writing into the installation directory.
Hana Sevcikova
tfr.map
## Not run:
##########################
# This example only makes sense if there is a simulation
# for all countries. Below, only two countries are included,
# so the map is useless.
##########################
sim.dir <- file.path(find.package("bayesPop"), "ex-data", "Pop")
pred <- get.pop.prediction(sim.dir=sim.dir, write.to.cache=FALSE)
# Using ggplot2
pop.ggmap(pred)
pop.ggmap(pred, year = 2100)
# Using rworldmap
# Uses heat colors with seven categories by default
pop.map(pred, sex="female", age=4:10)
# Female population in child-bearing age as a proportion of totals
pop.map(pred, expression="PXXX_F[4:10] / PXXX")
# The same with more colors
params <- get.pop.map.parameters(pred, expression="PXXX_F[4:10] / PXXX")
do.call("pop.map", params)
# Another projection year on the same color scale
do.call("pop.map", c(list(year=2043), params))
# Interactive map of potential support ratio (requires Flash)
pop.map.gvis(pred, expression="PXXX[5:13] / PXXX[14:27]")
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.