geom_lexis {ggpointless} | R Documentation |
Display events of different cohorts in form of a lexis charts
Description
This geom can be used to plot 45° lifelines for a cohort. Lexis diagrams are named after Wilhelm Lexis and used by demographers for more than a century.
Usage
geom_lexis(
mapping = NULL,
data = NULL,
...,
point_show = TRUE,
point_colour = NULL,
point_size = deprecated(),
gap_filler = TRUE,
lineend = "round",
linejoin = "round",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
stat_lexis(
mapping = NULL,
data = NULL,
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
Arguments
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
... |
Other arguments passed on to |
point_show |
logical. Should a point be shown at the end of each segment? TRUE by default |
point_colour |
color of a point |
point_size |
deprecated, use |
gap_filler |
logical. Should gaps be filled? TRUE by default |
lineend |
line end style (round, butt, square) |
linejoin |
line join style (round, mitre, bevel) |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
Details
This geom draws 45° lines from the start to the end of a 'lifetime'. It is
a combination of a segment, and a point.
Besides y
and yend
coordinates this geom creates one additional variable
called type
in the layer data. You might want to map to an aesthetic with
ggplot2::after_stat()
, see Examples section and vignette("ggpointless")
for more details.
Rows in your data with either missing x
or xend
values will be removed
(your segments must start and end somewhere).
Aesthetics
geom_lexis() understands the following aesthetics (required aesthetics are in bold):
-
x
-
xend
alpha
color
fill
group
shape
size
linetype
linewidth
stroke
Examples
df1 <- data.frame(
key = c("A", "B", "B", "C", "D", "E"),
start = c(0, 1, 6, 5, 6, 9),
end = c(5, 4, 10, 9, 8, 11)
)
p <- ggplot(df1, aes(x = start, xend = end, color = key))
p +
geom_lexis()
p +
geom_lexis(gap_filler = FALSE)
p +
geom_lexis(aes(linetype = after_stat(type)),
point_show = FALSE
)
# change point appearance
p + geom_lexis(
point_colour = "black",
size = 3,
shape = 21,
fill = "white",
stroke = 1
)
# missing values will be removed
df2 <- data.frame(
key = c("A", "B", "B", "C", "D"),
start = c(0, 1, 7, 5, 6),
end = c(5, 4, 13, 9, NA)
)
ggplot(df2, aes(x = start, xend = end, color = key)) +
geom_lexis()
# Ideally, `x` values should be increasing, unlike
# in the next example
df3 <- data.frame(x = Sys.Date() - 0:2, xend = Sys.Date() + 1:3)
ggplot(df3, aes(x = x, xend = xend)) +
geom_lexis()
# If `x` is of class Date, `xend` can't be of class `POSIXt` or
# `POSIXct`. The error is thrown by the `scales::date_trans` function.
## Not run:
ggplot(
data.frame(x = Sys.Date(), xend = Sys.time()),
aes(x = x, xend = xend)
) +
geom_lexis()
## End(Not run)