pop.pyramid creates a classic pyramid using rectangles; pop.trajectories.pyramid creates one or more pyramids using vertical lines (possibly derived from population trajectories). They can be used to view a prediction object created with this package, or any user-defined sex- and age-specific dataset. For the latter, function get.bPop.pyramid should be used to translate user-defined data into a bayesPop.pyramid object.## S3 method for class 'bayesPop.prediction':
pop.pyramid(pop.object, country, year = NULL,
indicator = c("P", "B", "D"), pi = c(80, 95),
proportion = FALSE, age = 1:21, plot = TRUE, ...)
## S3 method for class 'bayesPop.pyramid':
pop.pyramid(pop.object, main = NULL, show.legend = TRUE,
pyr1.par = list(border='black', col=NA, density=NULL, height=0.9),
pyr2.par = list(border='plum1', col='plum1', density=20, height=0.2),
col.pi = NULL, ann = par('ann'), axes = TRUE, grid = TRUE,
cex.main = 0.9, cex.sub = 1, cex = 1, cex.axis = 1, ...)
pop.pyramidAll(pop.pred, year = NULL,
output.dir = file.path(getwd(), 'pop.pyramid'),
output.type = "png", verbose = FALSE, ...)
## S3 method for class 'bayesPop.prediction':
pop.trajectories.pyramid(pop.object, country, year = NULL, pi = c(80, 95),
nr.traj = NULL, proportion = FALSE, age = 1:21, plot = TRUE, ...)
## S3 method for class 'bayesPop.pyramid':
pop.trajectories.pyramid(pop.object, main = NULL, show.legend = TRUE,
col = rainbow, col.traj = 'grey', lwd = 2, ann = par('ann'), axes = TRUE,
grid = TRUE, cex.main = 0.9, cex.sub = 1, cex = 1, cex.axis = 1, ...)
pop.trajectories.pyramidAll(pop.pred, year = NULL,
output.dir = file.path(getwd(), 'pop.traj.pyramid'),
output.type = "png", verbose = FALSE, ...)
## S3 method for class 'bayesPop.pyramid':
plot(x, \dots)## S3 method for class 'bayesPop.prediction':
get.bPop.pyramid(data, country, year = NULL,
indicator = c("P", "B", "D"), pi = c(80, 95),
proportion = FALSE, age = 1:21, nr.traj = 0, sort.pi=TRUE, ...)
## S3 method for class 'data.frame':
get.bPop.pyramid(data, main.label = NULL, legend = 'observed',
is.proportion = FALSE, ages = NULL, pop.max = NULL,
LRmain = c('Male', 'Female'), LRcolnames = c('male', 'female'), CI = NULL, ...)
## S3 method for class 'matrix':
get.bPop.pyramid(data, \dots)
## S3 method for class 'list':
get.bPop.pyramid(data, main.label = NULL, legend = NULL, CI = NULL, \dots)
bayesPop.prediction or bayesPop.pyramid (see Value section).bayesPop.prediction.bayesPop.pyramid.bayesPop.prediction. For data frame and matrix, it must have columns defined by LRcolnames (pop.pyramid draws the first two, pop.trajectories.pyramid draws all of them.
In the TRUE the pyramid contains the distribution of rates of age-specific counts and population totals.FALSE, nothing is plotted. It can be used to retrieve the pyramid object without drawing it.height component should be a number between 0 (corresponds to a line) and 1pi.NULL, all trajectories are plotted, otherwise they are thinned evenly.data is a list, names of the list elements are taken as legend.data are proportions, or NA in which case the proportions are computed.data. If it is not given, the age labels are considered to be the row names of data.max(data) is taken.bayesPop.pyramid$CI object, see below.pop.pyramid, pop.trajectories.pyramid and get.bPop.pyramid return an object of class bayesPop.pyramid which is a list with the following components:LRcolnames (see below). There is one row per age group and the row names are used for labeling the y-axis. Names of the list elements are used in the legend.pyramid component and it is NULL if the corresponding pyramid does not have confidence intervals. Each element is a list with one element per probability interval whose names are the values of the intervals. Each element is again a list with components low and high which have the same structure as pyramid and contain the lower and upper bounds of the corresponding interval.CI, it is ordered the same way as the pyramid component and is NULL if the corresponding pyramid does not have any trajectories to be shown. Each element is again a list with two components, one for the left part and one for the right part of the pyramid. Their names correspond to LRcolnames and each of them is a matrix of size number of age categories x number of trajectories. This is only used by the pop.trajectories.pyramid function.pyramid, CI and trajectories used to plot data into the left and right panel, respectively.pop.pyramid function generates one or two population pyramids in one plot. The first (main) one is usually the median of a future year prediction, but it can also be the current year or any population estimates. The second one serves the purpose of comparing two pyramids with one another and is drawn on top of the main pyramid. For example, one can use it to compare a future prediction with the present, or two different time points in the past, or two different geographies. The main pyramid can have confidence intervals associated with it, which are also plotted. If pop.pyramid is called on a bayesPop.prediction object, the main and secondary pyramid, respectively, is generated from data of a time period given by the first and second element, respectively, of the year argument. In such a case, confidence intervals only of the first year are shown. Thus, it makes sense to set the first year to be a prediction year and the second year to an observed time period. If pop.pyramid is called on a bayesPop.pyramid object, data in the first and second element, respectively, of the bayesPop.pyramid$pyramid list are used, and only the first element of bayesPop.pyramid$CI is used.Pyramids generated via the pop.trajectories.pyramid function have different appearance and therefore more than two pyramids can be put into one figure. Furthermore, confidence intervals of more than one pyramid can be shown. Thus, all elements of bayesPop.pyramid$pyramid and bayesPop.pyramid$CI are plotted. In addition, single trajectories given in bayesPop.pyramid$trajectories can be shown by setting the argument nr.traj larger than 0.
Both, pop.pyramid and pop.trajectories.pyramid (if called with a bayesPop.prediction object) use data from one country.
Functions pop.pyramidAll and pop.trajectories.pyramidAll create such pyramids for all countries for which a projection is available and for all years given by the year argument which should be a list. In this case, one pyramid figure (possibly containing multiple pyramids) is created for each country and each element of the year list.
The core of these functions operates on a bayesPop.pyramid object which is automatically created when called with a bayesPop.prediction object. If used with a user-defined data set, one has to convert such data into bayesPop.pyramid using the function get.bPop.pyramid (see an example below). In such a case, one can simply use the plot function which then calls pop.pyramid.
pop.trajectories.plot, bayesPop.prediction, summary.bayesPop.prediction# pyramids for bayesPop prediction objects
##########################################
sim.dir <- file.path(find.package("bayesPop"), "ex-data", "Pop")
pred <- get.pop.prediction(sim.dir)
pop.pyramid(pred, "Netherlands", c(2045, 2010))
dev.new()
pop.trajectories.pyramid(pred, "Netherlands", c(2045, 2010, 1960), age=1:25, proportion=TRUE)
# using manual manipulation of the data: e.g. show only the prob. intervals
pred.pyr <- get.bPop.pyramid(pred, country="Ecuador", year=2090, age=1:27)
pred.pyr$pyramid <- NULL
plot(pred.pyr)
# pyramids for user-defined data
################################
# this example dataset contains population estimates for the Washington state and King county
# (Seattle area) in 2011
data <- read.table(file.path(find.package("bayesPop"), "ex-data", "popestimates_WAKing.txt"),
header=TRUE, row.names=1)
# extract data for two pyramids and put it into the right format
head(data)
WA <- data[,c('WA.male', 'WA.female')]; colnames(WA) <- c('male', 'female')
King <- data[,c('King.male', 'King.female')]; colnames(King) <- c('male', 'female')
# create and plot a bayesPop.pyramid object
pyramid <- get.bPop.pyramid(list(WA, King), legend=c('Washington', 'King'))
plot(pyramid, main='Population in 2011', pyr2.par=list(height=0.7, col='violet', border='violet'))
# show data as proportions
pyramid.prop <- get.bPop.pyramid(list(WA, King), is.proportion=NA, legend=c('Washington', 'King'))
pop.pyramid(pyramid.prop, main='Population in 2011 (proportions)',
pyr1.par=list(col='lightgreen', border='lightgreen', density=30),
pyr2.par=list(col='darkred', border='darkred', density=50))Run the code above in your browser using DataLab