ggseqdplot | R Documentation |
Function for rendering state distribution plots with ggplot2
\insertCitewickham2016ggseqplot instead of base R's plot
function that is used by TraMineR::seqplot
\insertCitegabadinho2011ggseqplot.
ggseqdplot(
seqdata,
no.n = FALSE,
group = NULL,
dissect = NULL,
weighted = TRUE,
with.missing = FALSE,
border = FALSE,
with.entropy = FALSE,
linetype = "dashed",
linecolor = "black",
linewidth = 1,
facet_ncol = NULL,
facet_nrow = NULL,
...
)
seqdata |
State sequence object (class |
no.n |
specifies if number of (weighted) sequences is shown (default is |
group |
A vector of the same length as the sequence data indicating group membership. When not NULL, a distinct plot is generated for each level of group. |
dissect |
if |
weighted |
Controls if weights (specified in |
with.missing |
Specifies if missing states should be considered when computing the state distributions (default is |
border |
if |
with.entropy |
add line plot of cross-sectional entropies at each sequence position |
linetype |
The linetype for the entropy subplot ( |
linecolor |
Specifies the color of the entropy line if |
linewidth |
Specifies the width of the entropy line if |
facet_ncol |
Number of columns in faceted (i.e. grouped) plot |
facet_nrow |
Number of rows in faceted (i.e. grouped) plot |
... |
if group is specified additional arguments of |
Sequence distribution plots visualize the distribution of all states
by rendering a series of stacked bar charts at each position of the sequence.
Although this type of plot has been used in the life course studies for several
decades (see \insertCiteblossfeld1987;textualggseqplot for an early application),
it should be noted that the size of the different bars in stacked bar charts
might be difficult to compare - particularly if the alphabet comprises many
states \insertCitewilke2019ggseqplot. This issue can be addressed by breaking down
the aggregated distribution specifying the dissect
argument. Moreover, it
is important to keep in mind that this plot type does not visualize individual
trajectories; instead it displays aggregated distributional information
(repeated cross-sections). For a more detailed discussion of this type of
sequence visualization see, for example, \insertCitebrzinsky-fay2014;textualggseqplot,
\insertCitefasang2014;textualggseqplot, and \insertCiteraab2022;textualggseqplot.
The function uses TraMineR::seqstatd
to obtain state
distributions (and entropy values). This requires that the input data (seqdata
)
are stored as state sequence object (class stslist
) created with
the TraMineR::seqdef
function. The state distributions
are reshaped into a a long data format to enable plotting with ggplot2
.
The stacked bars are rendered by calling geom_bar
; if entropy = TRUE
entropy values are plotted with geom_line
. If the group
or the
dissect
argument are specified the sub-plots are produced by using
facet_wrap
. If both are specified the plots are rendered with
facet_grid
.
The data and specifications used for rendering the plot can be obtained by storing the
plot as an object. The appearance of the plot can be adjusted just like with
every other ggplot (e.g., by changing the theme or the scale using +
and
the respective functions).
A sequence distribution plot created by using ggplot2
.
If stored as object the resulting list object (of class gg and ggplot) also
contains the data used for rendering the plot.
Marcel Raab
# Use example data from TraMineR: actcal data set
data(actcal)
# We use only a sample of 300 cases
set.seed(1)
actcal <- actcal[sample(nrow(actcal), 300), ]
actcal.lab <- c("> 37 hours", "19-36 hours", "1-18 hours", "no work")
actcal.seq <- seqdef(actcal, 13:24, labels = actcal.lab)
# state distribution plots; grouped by sex
# with TraMineR::seqplot
seqdplot(actcal.seq, group = actcal$sex)
# with ggseqplot
ggseqdplot(actcal.seq, group = actcal$sex)
# with ggseqplot applying a few additional arguments, e.g. entropy line
ggseqdplot(actcal.seq, group = actcal$sex,
no.n = TRUE, with.entropy = TRUE, border = TRUE)
# break down the stacked plot to ease comparisons of distributions
ggseqdplot(actcal.seq, group = actcal$sex, dissect = "row")
# make use of ggplot functions for modifying the plot
ggseqdplot(actcal.seq) +
scale_x_discrete(labels = month.abb) +
labs(title = "State distribution plot", x = "Month") +
guides(fill = guide_legend(title = "Alphabet")) +
theme_classic() +
theme(plot.title = element_text(size = 30,
margin = margin(0, 0, 20, 0)),
plot.title.position = "plot")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.