TraMineR seqdplot:按总频率排序状态

TraMineR seqdplot : order states by total frequency

我想显示按状态频率排序的状态分布图(通过 seqdplot 获得)。 例如,使用 mvad 数据集:

library(TraMineR)
data(mvad)

mvad.labels <- c("employment", "further education", "higher education", "joblessness", "school", "training")
mvad.scode <- c("EM", "FE", "HE", "JL", "SC", "TR")
mvad.seq <- seqdef(mvad, 17:86, states = mvad.scode,labels = mvad.labels, xtstep = 6)

seqdplot(mvad.seq, border = NA, title = "State distribution plot")

我想按照全局频率的顺序绘制状态(例如,最频繁的状态在图的底部,最不频繁的在顶部,或者以任何指定的顺序排列) . seqdplot 函数可以吗? 或者你们知道其他选择吗? (ggplot2..)

TraMineR 图保留了状态序列对象的状态顺序。要更改顺序,您必须使用所需的状态顺序定义一个新的状态序列对象。

您可以通过 seqmeant 获得状态频率。一旦定义了顺序,就可以通过在 seqdef 中明确定义字母表来强加它。您还必须相应地对短标签和长标签以及状态颜色进行排序。

这是你可以做到的

## Get state freq with seqmeant
mt <- seqmeant(mvad.seq)
## order of frequencies
ord <- order(mt, decreasing = TRUE)

## Sorted alphabet
alph.s <- rownames(mt)[ord]
## we need also to sort accordingly labels and colors
mvad.labels.s <- mvad.labels[ord]
mvad.scode.s <- mvad.scode[ord]
cpal.s <- cpal(mvad.seq)[ord]

## Define sequence object with sorted states
mvad.seq.s <- seqdef(mvad.seq, alphabet = alph.s, states = mvad.scode.s,
                   labels = mvad.labels.s, cpal = cpal.s, xtstep = 6)

seqdplot(mvad.seq.s, border = NA, main = "State distribution plot")