*! version 1.2

help file for pyramid_chart

Title

pyramid_chart

Description

pyramid_chart generates a population pyramid chart for a given numeric variable, with categories defined by two categorical variables. The chart displays the distribution of the numeric variable across the levels of the first categorical variable, split by the levels of the second categorical variable.

Syntax

pyramid_chart varname [if] [in] , over(varname) by(varname) dec(integer) [scatter options] [twoway_options]

Options

over(varname) specifies the categorical variable that defines the y-axis categories.

by(varname) specifies the categorical variable that splits the data into two groups (e.g., male and female).

dec(integer) specifies the number of decimal places for the percentage labels on the bars of the pyramid. Note that this does not affect the decimal points in the x axis which are fixed as integers.

Remarks

pyramid_chart can only accept one variable in the varname following the pyramid_chart command.

The pyramid_chart command requires that the levels of the variable specified in the by() option be labeled, with labels for categories 1 and 2.

It is important that the dataset has a numeric variable in varname, a categorical variable in over(), a categorical variable in by(), and the dataset is long. If it is wide, first perform reshape before calling the pyramid_chart command.

It is possible to manage color settings for bar labels using scatter options.

Examples

Pre-analysis:

While reshaping is not required before producing a pyramid chart, it is important that the data is in long format before it is fed into the pyramid_chart command. As an illustration, the following code reshapes the system-included pop2000.dta dataset into a long dataset containing the three required variables: population, sex, and agegrp.

sysuse pop2000.dta, clear
keep agegrp maletotal femtotal
rename maletotal population1
rename femtotal population2
reshape long population, i(agegrp) j(sex)
label define sexlbl 1 "Male" 2 "Female"
label values sex sexlbl
label variable population "Population"
label variable sex "Sex"

Example 1:

A basic pyramid chart with minimum mandatory input, and using system generated default options.

pyramid_chart population, over(agegrp) by(sex) dec(0)

Example 2:

Add titles to the graph, add 1 decimal place to the bar labels.

pyramid_chart population, over(agegrp) by(sex) dec(1) title("Population Pyramid for year 2000") subtitle("in percentages") xtitle("Percentage") ytitle("Age groups")

Example 3:

Add scatterplot options (for example, for color selection of bar labels).

pyramid_chart population, over(agegrp) by(sex) dec(1) sctopt(mlabcolor(blue))

Example 4:

Add various schemes to the graph.

pyramid_chart population, over(agegrp) by(sex) dec(1) scheme(white_tableau)

Authors

Written by Masud Rahman, Economist, UNHCR. For queries, message on twiiter.

Also see

graph, twoway, twoway_options reshape

Contributing

pyramid_chart is open for development on GitHub under MIT license. Feel free to request features, or contribute to the package by submitting pull requests.

Additional contributors till current version: Fernando Rios-Avila.