在TraMineR中使用seqpcplot时,在y轴上保持标签顺序

时间:2015-06-17 15:45:32

标签: r traminer

我正在使用R包TraMineR。我想使用命令seqpcplot绘制频繁的事件序列。我之前编写了alphabet中的状态,以便按字母顺序对它们进行编码,以便在使用seqdef命令计算序列时不指定labelsstates选项我获得了以下输出:

[>] state coding:
       [alphabet]  [label]  [long label] 
     1  a.sin       a.sin    a.sin
     2  b.co0       b.co0    b.co0
     3  c.co1       c.co1    c.co1
     4  d.co2+      d.co2+   d.co2+
     5  e.ma0       e.ma0    e.ma0
     6  f.ma1       f.ma1    f.ma1
     7  g.ma2+      g.ma2+   g.ma2+
     8  h.sin0      h.sin0   h.sin0
     9  i.lp1       i.lp1    i.lp1
     10  l.lp2+      l.lp2+   l.lp2+
     11  m.lp1_18    m.lp1_18 m.lp1_18
     12  n.lp2_18    n.lp2_18 n.lp2_18

然后我使用seqecreate在事件序列对象中转换状态序列对象。当通过seqpcplot绘制事件序列时,我获得了一个非常好的图表,其中状态按照alphabet在y轴上按字母顺序排序。

但是,我想在图表中使用更长的标签,以便我在labels命令中指定statesseqdef选项为

lab<-c("single", "cohabNOchildren","cohab1child","cohab2+children","marrNOchildren","marr1child","marr2+children","singleNOchildren","loneMother1child","loneMother2+children","loneMother1child_over18","loneMother2+children_over18")

获得:

[>] state coding:
       [alphabet]  [label]                     [long label] 
     1  a.sin       single                      single
     2  b.co0       cohabNOchildren             cohabNOchildren
     3  c.co1       cohab1child                 cohab1child
     4  d.co2+      cohab2+children             cohab2+children
     5  e.ma0       marrNOchildren              marrNOchildren
     6  f.ma1       marr1child                  marr1child
     7  g.ma2+      marr2+children              marr2+children
     8  h.sin0      singleNOchildren            singleNOchildren
     9  i.lp1       loneMother1child            loneMother1child
     10  l.lp2+      loneMother2+children        loneMother2+children
     11  m.lp1_18    loneMother1child_over18     loneMother1child_over18
     12  n.lp2_18    loneMother2+children_over18 loneMother2+children_over18

和以前一样,我然后使用seqpcplot

计算事件序列并绘制它们
seqpcplot(example.seqe, 
          filter = list(type = "function",
                  value = "cumfreq",
                  level = 0.8),
   order.align = "last",
   ltype = "non-embeddable",
   cex = 1.5, lwd = .9,
   lcourse = "downwards")

这次y轴上的状态是状态按字母顺序排序,但遵循labelsstates标签而不是alphabet给出的顺序,正如我所希望的那样。

当指定了alphabetseqpcplot选项并且可能遵循不同的字母顺序时,是否可以在labels时使用states保留字母顺序来自alphabet

的订单

感谢。

2 个答案:

答案 0 :(得分:2)

我同意上述解决方案。作为补充,这里有许多可能的解决方案:

seqecreate中使用alphabetseqpcplot参数:

dat <- data.frame(id = factor(1, 1, 1),
                  timestamp = c(0, 20, 22),
                  event = factor(c("A", "B", "C")))
dat.seqe <- seqecreate(dat)
seqpcplot(dat.seqe, alphabet = c("C", "A", "B"))

仅使用seqecreate

dat <- data.frame(id = factor(1, 1, 1),
                  timestamp = c(0, 20, 22),
                  event = factor(c("A", "B", "C"),levels = c("C", "A", "B")))
dat.seqe <- seqecreate(dat)
seqpcplot(dat.seqe)

使用seqdef(此处原始类别与要在y轴上显示的标签不同)

dat <- data.frame(id = factor(1),
                  ev.0 = factor("AA", levels = c("CC", "AA", "BB")),
                  ev.20 = factor("BB", levels = c("CC", "AA", "BB")),
                  ev.22 = factor("CC", levels = c("CC", "AA", "BB")))
dat.seq <- seqdef(dat, var = 2:4, alphabet = c("CC", "AA", "BB"),
                  states = c("C", "A", "B"))
seqpcplot(dat.seq)

最后一个解决方案可能是您正在寻找的解决方案。希望它有所帮助。

答案 1 :(得分:1)

alphabet函数的seqpcplot参数用于控制该顺序。像

这样的东西
seqpcplot(example.seqe,
   alphabet = lab, 
   filter = list(type = "function",
                  value = "cumfreq",
                  level = 0.8),
   order.align = "last",
   ltype = "non-embeddable",
   cex = 1.5, lwd = .9,
   lcourse = "downwards")

应该给你预期的情节。