Keeping the label order on the y-axis when using seqpcplot in TraMineR

143 Views Asked by At

I'm using the R package TraMineR. I would like to plot frequent event sequences by using the command seqpcplot. I previously coded the states in the alphabet as to keep them in alphabetical order so that when I compute the sequences by using the seqdef command without specifying the labels and states options I obtain the following output:

[>] 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

I then convert the state-sequence objet in an event-sequece objet by using seqecreate. When plotting the event sequences by seqpcplot I obtain a very nice graph where the states are ordered alphabetically on the y-axis according to the alphabet.

However, I would like to use longer labels in the graphs, so that I specified the labels and states options in the seqdef command as

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

obtaining:

[>] 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

As before, I then computed the event sequences and plot them by using 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")

This time the states on the y-axis were the states are ordered alphabetically but following the order given by the labels and states labels rather than the alphabet, as I wished.

Is there a way to keep the alphabetical order given in the alphabet when plotting with seqpcplot when the labels and states options are specified and may follow a different alphabetical order from the alphabet?

Thanks.

2

There are 2 best solutions below

1
On BEST ANSWER

I agree with the solution above. As a supplement, here a number of possible solutions:

Using seqecreate and the alphabet argument in seqpcplot:

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"))

Using seqecreate only

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)

Using seqdef (here the original categories are different than the labels to be shown in the y-axis)

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)

The last solution may be the one you're looking for. Hope it helps.

2
On

The alphabet argument of the seqpcplot function is there to control that order. Something like

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")

should give you the expected plot.