TraMineR function seqfplot() yaxis: show percentage value for each sequence

98 views Asked by At

I used to use TraMineR function seqfplot() with argument yaxis = "pct", which nicely put the percentage value for each sequence on the y-axis. Somewhere along the way some update of the package lost that feature somehow (I am on version 2.2-7 now), and an error is thrown.

library(TraMineR) # version 2.2-7

data from example in ?seqplot:

data(actcal)
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)

simple plot, but not with the y-axis annotiation I want (the percentage value for each sequence should be displayed):

seqfplot(actcal.seq)

this used to work in earlier versions of TraMineR, these days it throws an Error: If not logical, yaxis should be one of "all" or "left"

seqfplot(actcal.seq, yaxis = "pct")

when using ?plot.stslist.freq directly, it does what seqfplot used to do:

x <- seqtab(actcal.seq)
plot(x, yaxis = "pct")

But this doesn't have all the sophisticated features of seqfplot (like allowing grouping and automatic display of the state color legend). The problem seems to be that argument yaxis is used in both seqfplot() and in plot.stslist.freq(), but in different ways, and so cannot be passed through.

Can I somehow get seqfplot(actcal.seq, yaxis = "pct") to work as it used to?

1

There are 1 answers

0
maraab On

Sounds like a bug after one of the recent TraMineR updates. I am confident that the excellent TraMineR team will fix the issue once they are aware of it (either due to your question or a direct message).

In the meantime, I'd like to point you to an alternative package for plotting sequence data, I wrote because I am usually using ggplot2 instead of base R plot. Maybe, that's an interesting option for you as well.

library(TraMineR)
library(ggseqplot)
library(TraMineR)
library(ggplot2)

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


# sequence frequency plot with ggseqplot
ggseqfplot(actcal.seq,
           ylabs = "share") # note: overlapping labels are removed automatically


# apply grouping and some other sophistaced stuff
ggseqfplot(actcal.seq,
           group = actcal$sex,
           ranks = 1:5,
           ylabs = "share") +
  scale_x_discrete(breaks = 1:12,
                   labels = month.abb,
                   guide = guide_axis(n.dodge=2),
                   expand = expansion(add = c(0.2, 0)))
#> Scale for x is already present.
#> Adding another scale for x, which will replace the existing scale.

Created on 2023-05-04 with reprex v2.0.2