| Title: | Plot Lexis Diagrams for Demographic Purposes |
|---|---|
| Description: | Plots empty Lexis grids, adds lifelines and highlights certain areas of the grid, like cohorts and age groups. |
| Authors: | Philipp Ottolinger [cre, aut], Marieke Smilde-Becker [ctb] |
| Maintainer: | Philipp Ottolinger <[email protected]> |
| License: | GPL-2 |
| Version: | 0.4.0 |
| Built: | 2026-06-09 08:06:23 UTC |
| Source: | https://github.com/ottlngr/lexisplotr |
Add a coloured rectangle to an existing Lexis grid to highlight a certain age in that Lexis grid.
lexis_age(lg, age, delta = 1, fill = lexisplotr_colours()[1], alpha = 0.7)lexis_age(lg, age, delta = 1, fill = lexisplotr_colours()[1], alpha = 0.7)
lg |
an existing object originally created with |
age |
numeric, set the age to highlight. |
delta |
numeric, set the size of the age groups. Default is 1. |
fill |
character, set colour to fill the rectangle. |
alpha |
numeric, set alpha, the level of transparency for |
Takes an existing Lexis grid and adds a coloured rectangle that highlights all triangles belonging to a certain age.
A ggplot2 object.
Philipp Ottolinger
library(LexisPlotR) lexis <- lexis_grid(year_start = 1900, year_end = 1905, age_start = 0, age_end = 5) lexis <- lexis_age(lg = lexis, age = 3)library(LexisPlotR) lexis <- lexis_grid(year_start = 1900, year_end = 1905, age_start = 0, age_end = 5) lexis <- lexis_age(lg = lexis, age = 3)
Takes an existing Lexis grid and adds a coloured rectangle to highlight a certain cohort.
lexis_cohort(lg, cohort, delta = 1, fill = lexisplotr_colours()[3], alpha = 0.7)lexis_cohort(lg, cohort, delta = 1, fill = lexisplotr_colours()[3], alpha = 0.7)
lg |
an existing object originally created with |
cohort |
numeric, set the cohort to highlight. |
delta |
numeric, set the size of the age groups. Default is 1. |
fill |
character, set the colour of the rectangle. |
alpha |
numeric, set the level of transparency of the rectangle. Default is |
Takes an existing Lexis grid and adds a coloured rectangle to the plot. The rectangle will highlight a certain cohort in the Lexis grid.
Philipp Ottolinger
library(LexisPlotR) lg <- lexis_grid(year_start = 1900, year_end = 1905, age_start = 0, age_end = 5) lexis_cohort(lg = lg, cohort = 1901)library(LexisPlotR) lg <- lexis_grid(year_start = 1900, year_end = 1905, age_start = 0, age_end = 5) lexis_cohort(lg = lg, cohort = 1901)
lexis_grid() plots the basic Lexis grid.
lexis_grid(year_start, year_end, age_start, age_end, delta = 1, lwd = 0.3, force_equal = TRUE)lexis_grid(year_start, year_end, age_start, age_end, delta = 1, lwd = 0.3, force_equal = TRUE)
year_start |
integer, set the year the Lexis Diagram starts with. |
year_end |
integer, set the year the Lexis Diagram ends with. |
age_start |
integer, set the age the Lexis Diagram starts with. |
age_end |
integer, set the age the Lexis Diagram ends with. |
delta |
numeric, set the size of the age groups. Default is 1. |
lwd |
numeric, set the linewidth of the grid. |
force_equal |
logical, by default |
The function determines the aspect ratio of the x- and y-axis to enforce
isosceles triangles. The aspect ratio will not be effected by defining
width and height in pdf() or other graphic devices.
Because the returned object is a ggplot2 graph, the overall appearence of
the graph can be edited by adding themes() to the plot.
A ggplot object.
Philipp Ottolinger
library(LexisPlotR) lexis_grid(year_start = 1900, year_end = 1905, age_start = 0, age_end = 5)library(LexisPlotR) lexis_grid(year_start = 1900, year_end = 1905, age_start = 0, age_end = 5)
Add lifelines to an existing Lexis grid.
lexis_lifeline(lg, birth, entry = NA, exit = NA, lineends = FALSE, colour = lexisplotr_colours()[5], alpha = 1, lwd = 0.5)lexis_lifeline(lg, birth, entry = NA, exit = NA, lineends = FALSE, colour = lexisplotr_colours()[5], alpha = 1, lwd = 0.5)
lg |
an existing object originally created with |
birth |
character, set the birth date of an individual in format |
entry |
character, set the entry of an individual in format |
exit |
character, set the exit or death date of an individual in format |
lineends |
logical, if |
colour |
character, set the colour of the lifelines. |
alpha |
numeric, set the transparency of the lifelines. Default is |
lwd |
numeric, set the linewidth of the lifelines. Default is |
Takes an existing Lexis grid and adds lifelines to the grid. Input can be a single dates or dates from a vector.
A ggplot2 object.
Philipp Ottolinger
lg <- lexis_grid(year_start = 1900, year_end = 1905, age_start = 0, age_end = 5) lexis_lifeline(lg = lg, birth = "1901-09-23") lexis_lifeline(lg = lg, birth = "1901-09-23", entry = "1902-04-01") lexis_lifeline(lg = lg, birth = "1901-09-23", exit = "1904-10-31")lg <- lexis_grid(year_start = 1900, year_end = 1905, age_start = 0, age_end = 5) lexis_lifeline(lg = lg, birth = "1901-09-23") lexis_lifeline(lg = lg, birth = "1901-09-23", entry = "1902-04-01") lexis_lifeline(lg = lg, birth = "1901-09-23", exit = "1904-10-31")
Plot a polygon inside a Lexis grid Takes an existing Lexis grid and adds a polygon.
lexis_polygon(lg, x, y, group = 1, fill = lexisplotr_colours()[4], alpha = 0.7)lexis_polygon(lg, x, y, group = 1, fill = lexisplotr_colours()[4], alpha = 0.7)
lg |
an existing object originally created with |
x |
vector describing the x coordinates of the polygon. Format: YYYY-MM-DD. |
y |
vector describing the y coordinates of the polygon |
group |
vector describing the groups of coordinates. |
fill |
character, fill colour of the polygon. |
alpha |
numeric, transparency of the fill colour. Default: 0.7. |
The function can be used to plot a polygon inside a Lexis grid.
Philipp Ottolinger
## Not run: library(LexisPlotR) lg <- lexis_grid(year_start = 1900, year_end = 1905, age_start = 0, age_end = 5) lexis_polygon(lg, x = c("1901-06-30", "1904-06-30", "1904-06-30", "1901-06-30"), y = c(2,2,4,4)) ## End(Not run)## Not run: library(LexisPlotR) lg <- lexis_grid(year_start = 1900, year_end = 1905, age_start = 0, age_end = 5) lexis_polygon(lg, x = c("1901-06-30", "1904-06-30", "1904-06-30", "1901-06-30"), y = c(2,2,4,4)) ## End(Not run)
Takes an existing Lexis grid and adds a coloured rectangle to highlight a certain age.
lexis_year(lg, year, delta = 1, fill = lexisplotr_colours()[2], alpha = 0.7)lexis_year(lg, year, delta = 1, fill = lexisplotr_colours()[2], alpha = 0.7)
lg |
an existing object originally created with |
year |
numeric, set the year to highlight. |
delta |
numeric, set the size of the age groups. Default is 1. |
fill |
character, set the colour of the rectangle. |
alpha |
numeric, set the transparency of the rectangle. Default is |
Takes an existing Lexis grid and adds a coloured rectangle to the plot. The rectangle will highlight a certain year in the grid.
A ggplot2 object.
Philipp Ottoliner
lg <- lexis_grid(year_start = 1900, year_end = 1905, age_start = 0, age_end = 5) lexis_year(lg = lg, year = 1902)lg <- lexis_grid(year_start = 1900, year_end = 1905, age_start = 0, age_end = 5) lexis_year(lg = lg, year = 1902)
Add a coloured rectangle to an existing Lexis grid to highlight a certain age in that Lexis grid.
lexis.age(lg, age, fill = lpr_colours()[2], alpha = 0.7, d = 1)lexis.age(lg, age, fill = lpr_colours()[2], alpha = 0.7, d = 1)
lg |
an existing object originally created with |
age |
numeric, set the age to highlight. |
fill |
character, set colour to fill the rectangle. Default is |
alpha |
numeric, set alpha, the level of transparency for |
d |
numeric, set the size of the age groups. Default is 1. |
Takes an existing Lexis grid and adds a coloured rectangle that highlights all triangles belonging to a certain age.
A ggplot2 object.
Philipp Ottolinger
## Not run: library(LexisPlotR) lexis <- lexis.grid2(year.start = 1900, year.end = 1905, age.start = 0, age.end = 5) lexis <- lexis.age(lg = lexis, age = 3) ## End(Not run)## Not run: library(LexisPlotR) lexis <- lexis.grid2(year.start = 1900, year.end = 1905, age.start = 0, age.end = 5) lexis <- lexis.age(lg = lexis, age = 3) ## End(Not run)
Takes an existing Lexis grid and adds a coloured rectangle to highlight a certain cohort.
lexis.cohort(lg, cohort, fill = lpr_colours()[4], alpha = 0.7, d = 1)lexis.cohort(lg, cohort, fill = lpr_colours()[4], alpha = 0.7, d = 1)
lg |
an existing object originally created with |
cohort |
numeric, set the cohort to highlight. |
fill |
character, set the colour of the rectangle. Default is |
alpha |
numeric, set the level of transparency of the rectangle. Default is |
d |
numeric, set the size of the age groups. Default is 1. |
Takes an existing Lexis grid and adds a coloured rectangle to the plot. The rectangle will highlight a certain cohort in the Lexis grid.
Philipp Ottolinger
## Not run: library(LexisPlotR) lg <- lexis.grid(year.start = 1900, year.end = 1905, age.start = 0, age.end = 5) lexis.cohort(lg = lg, cohort = 1901) ## End(Not run)## Not run: library(LexisPlotR) lg <- lexis.grid(year.start = 1900, year.end = 1905, age.start = 0, age.end = 5) lexis.cohort(lg = lg, cohort = 1901) ## End(Not run)
lexis.grid() plots the basic Lexis grid.
lexis.grid(year.start, year.end, age.start, age.end, lwd = 0.3, force.equal = T)lexis.grid(year.start, year.end, age.start, age.end, lwd = 0.3, force.equal = T)
year.start |
integer, set the year the Lexis Diagram starts with. |
year.end |
integer, set the year the Lexis Diagram ends with. |
age.start |
integer, set the age the Lexis Diagram starts with. |
age.end |
integer, set the age the Lexis Diagram ends with. |
lwd |
numeric, set the linewidth of the grid. |
force.equal |
logical, by default |
The function determines the aspect ratio of the x- and y-axis to enforce
isosceles triangles. The aspect ratio will not be effected by defining
width and height in pdf() or other graphic devices.
Because the returned object is a ggplot2 graph, the overall appearence of
the graph can be edited by adding themes() to the plot.
The functions returns a ggplot2-plot.
Philipp Ottolinger
## Not run: library(LexisPlotR) lexis.grid(year.start = 1900, year.end = 1905, age.start = 0, age.end = 5) ## End(Not run)## Not run: library(LexisPlotR) lexis.grid(year.start = 1900, year.end = 1905, age.start = 0, age.end = 5) ## End(Not run)
lexis.grid() plots the basic Lexis grid.
lexis.grid2(year.start, year.end, age.start, age.end, lwd = 0.3, force.equal = T, d = 1)lexis.grid2(year.start, year.end, age.start, age.end, lwd = 0.3, force.equal = T, d = 1)
year.start |
integer, set the year the Lexis Diagram starts with. |
year.end |
integer, set the year the Lexis Diagram ends with. |
age.start |
integer, set the age the Lexis Diagram starts with. |
age.end |
integer, set the age the Lexis Diagram ends with. |
lwd |
numeric, set the linewidth of the grid. |
force.equal |
logical, by default |
d |
numeric, set the size of the age groups. Default is 1. |
The function determines the aspect ratio of the x- and y-axis to enforce
isosceles triangles. The aspect ratio will not be effected by defining
width and height in pdf() or other graphic devices.
Because the returned object is a ggplot2 graph, the overall appearence of
the graph can be edited by adding themes() to the plot.
The functions returns a ggplot2-plot.
Philipp Ottolinger
## Not run: library(LexisPlotR) lexis.grid(year.start = 1900, year.end = 1905, age.start = 0, age.end = 5) ## End(Not run)## Not run: library(LexisPlotR) lexis.grid(year.start = 1900, year.end = 1905, age.start = 0, age.end = 5) ## End(Not run)
The function opens an existing Lexis grid and fill the triangles according to data from the Human Mortality Database.
lexis.hmd(lg, hmd.data, column)lexis.hmd(lg, hmd.data, column)
lg |
an existing object originally created with |
hmd.data |
a data.frame created with |
column |
character, the name of the column of |
The function creates a subset of hmd.data that fits in the dimensions of the existing Lexis grid.
The triangles will be filled according to the data in column.
Philipp Ottolinger
## Not run: library(LexisPlotR) lg <- lexis.grid(year.start = 1980, year.end = 1985, age.start = 0, age.end = 5) # Load sample data path <- system.file("extdata", "Deaths_lexis_sample.txt", package = "LexisPlotR") deaths.triangles <- prepare.hmd(path) lexis.hmd(lg = lg, hmd.data = deaths.triangles, column = "Total") ### Plot data not explicitly present in HMD data deaths.triangles$RatioMale <- deaths.triangles$Male / deaths.triangles$Total lexis.hmd(lg, deaths.triangles, "RatioMale") ## End(Not run)## Not run: library(LexisPlotR) lg <- lexis.grid(year.start = 1980, year.end = 1985, age.start = 0, age.end = 5) # Load sample data path <- system.file("extdata", "Deaths_lexis_sample.txt", package = "LexisPlotR") deaths.triangles <- prepare.hmd(path) lexis.hmd(lg = lg, hmd.data = deaths.triangles, column = "Total") ### Plot data not explicitly present in HMD data deaths.triangles$RatioMale <- deaths.triangles$Male / deaths.triangles$Total lexis.hmd(lg, deaths.triangles, "RatioMale") ## End(Not run)
Add lifelines to an existing Lexis grid.
lexis.lifeline(lg, entry, exit = NA, lineends = F, colour = lpr_colours()[7], alpha = 1, lwd = 0.5)lexis.lifeline(lg, entry, exit = NA, lineends = F, colour = lpr_colours()[7], alpha = 1, lwd = 0.5)
lg |
an existing object originally created with |
entry |
character, set the entry or birth date of an individual in format |
exit |
character, set the exit or death date of an individual in format |
lineends |
logical, if |
colour |
character, set the colour of the lifelines. Default is |
alpha |
numeric, set the transparency of the lifelines. Default is |
lwd |
numeric, set the linewidth of the lifelines. Default is |
Takes an existing Lexis grid and adds lifelines to the grid. Input can be a single dates or dates from a vector.
A ggplot2 object.
Philipp Ottolinger
## Not run: lg <- lexis.grid(year.start = 1900, year.end = 1905, age.start = 0, age.end = 5) lexis.lifeline(lg = lg, entry = "1901-09-23") lexis.lifeline(lg = lg, entry = "1901-09-23", exit = "1904-03-03") ## End(Not run)## Not run: lg <- lexis.grid(year.start = 1900, year.end = 1905, age.start = 0, age.end = 5) lexis.lifeline(lg = lg, entry = "1901-09-23") lexis.lifeline(lg = lg, entry = "1901-09-23", exit = "1904-03-03") ## End(Not run)
Deprecated. Emphasize a survey range in a Lexis grid Takes an existing Lexis grid and adds a coloured parallelogram to highlight a survey range.
lexis.survey(lg, from_date, to_date, from_age, to_age, fill = lpr_colours()[6], alpha = 0.7)lexis.survey(lg, from_date, to_date, from_age, to_age, fill = lpr_colours()[6], alpha = 0.7)
lg |
an existing object originally created with |
from_date |
character, set the beginning of the survey in format |
to_date |
character, set the end of the survey in format |
from_age |
numeric, set the starting age of the survey. |
to_age |
numeric, set the ending age of the survey. |
fill |
character, set the colour to fill the parallelogram. Default is |
alpha |
numeric, set the transparency of the fill colour. Default is |
The function can be used to plot the time and age range of a survey. Use from_date and to_date to specify the time range the survey took place and from_age and to_age to define the considered ages of participants/observations.
Philipp Ottolinger
## Not run: library(LexisPlotR) lg <- lexis.grid(year.start = 1980, year.end = 1990, age.start = 30, age.end = 40) lexis.survey(lg, from_date = "1982-09-01", to_date = "1986-03-01", from_age = 32, to_age = 36) ## End(Not run)## Not run: library(LexisPlotR) lg <- lexis.grid(year.start = 1980, year.end = 1990, age.start = 30, age.end = 40) lexis.survey(lg, from_date = "1982-09-01", to_date = "1986-03-01", from_age = 32, to_age = 36) ## End(Not run)
Takes an existing Lexis grid and adds a coloured rectangle to highlight a certain age.
lexis.year(lg, year, fill = lpr_colours()[3], alpha = 0.7, d = 1)lexis.year(lg, year, fill = lpr_colours()[3], alpha = 0.7, d = 1)
lg |
an existing object originally created with |
year |
numeric, set the year to highlight. |
fill |
character, set the colour of the rectangle. Default is |
alpha |
numeric, set the transparency of the rectangle. Default is |
d |
numeric, set the size of the age groups. Default is 1. |
Takes an existing Lexis grid and adds a coloured rectangle to the plot. The rectangle will highlight a certain year in the grid.
A ggplot2 object.
Philipp Ottoliner
## Not run: lg <- lexis.grid(year.start = 1900, year.end = 1905, age.start = 0, age.end = 5) lexis.year(lg = lg, year = 1902) ## End(Not run)## Not run: lg <- lexis.grid(year.start = 1900, year.end = 1905, age.start = 0, age.end = 5) lexis.year(lg = lg, year = 1902) ## End(Not run)
This dataset contains 300 random entry dates and 150 exit dates for demonstration purposes.
lifelines_samplelifelines_sample
A data frame with 300 rows and 2 variables:
entry or birth dates.
exit or death dates, NA if not observed.
lexis.hmd()
prepare.hmd() prepares the raw 'Deaths by Lexis triangles' HMD data for further use by lexis.hmd.
prepare.hmd(file)prepare.hmd(file)
file |
the name of the 'Deaths by Lexis triangles' file downloaded from the Human Mortality Database. |
This function reads the raw data into R and transforms data to numeric and Date.
Furthermore seven columns (upper, x1, x2, x3, y1, y2, y3) that contain the coordinates of the triangles will be added.
The age group 110+ will be removed from the data.
Philipp Ottolinger
## Not run: library(LexisPlotR) # Load sample data path <- system.file("extdata", "Deaths_lexis_sample.txt", package = "LexisPlotR") deaths.triangles <- prepare.hmd(path) ## End(Not run)## Not run: library(LexisPlotR) # Load sample data path <- system.file("extdata", "Deaths_lexis_sample.txt", package = "LexisPlotR") deaths.triangles <- prepare.hmd(path) ## End(Not run)
Take raw data from a data source (e.g. Human Mortality Database) and make it 'tidy'.
tidy_triangle_data(triangle_data, source = "HMD")tidy_triangle_data(triangle_data, source = "HMD")
triangle_data |
data.frame, A data.frame containing raw triangle data. |
source |
character, The source of the raw data. Supported sources: HMD. |
When using raw triangular data from HMD or other sources, the data is not well prepared for further use. tidy_triangular_data does some transformations to prepare the data, mainly for visualization using ggplot2.
A data.frame.
Philipp Ottolinger
## Not run: triangles <- readHMDweb("RUS", "Deaths_lexis", "[email protected]", "your_password") ## End(Not run)## Not run: triangles <- readHMDweb("RUS", "Deaths_lexis", "[email protected]", "your_password") ## End(Not run)