TraMineR - Seqiplot include more x axis labels and rotation

71 Views Asked by At

I would be very grateful for advice. I would would like to include all x-axis labels on ticks, reduce the font size so that they don't overlap and rotate them. In this example, there are 23 tick marks but only 12 1abels. I would like them all displayed. Many thanks!

data(mvad)
mvad=mvad%>%select(1:37)

mvad.labels <- c("employment", "further education", "higher education",
                 "joblessness", "school", "training")
mvad.scodes <- c("EM","FE","HE","JL","SC","TR")
mvad.seq <- seqdef(mvad, 15:37, states=mvad.scodes, labels=mvad.labels)
seqiplot(mvad.seq, withlegend=F)

1

There are 1 best solutions below

1
On

The code below provides different options to obtain the desired goal. Note that I have chosen a tiny font size to illustrate the point. You can either use TraMineR's seqplot command (which uses base R's plot environment) or ggseqplot (which uses ggplot2 internally).

library(TraMineR)
library(ggseqplot)
library(dplyr)


data(mvad)
mvad <- mvad |> select(1:37)

mvad.labels <- c("employment", "further education", "higher education",
                 "joblessness", "school", "training")
mvad.scodes <- c("EM","FE","HE","JL","SC","TR")
mvad.seq <- seqdef(mvad, 15:37, states=mvad.scodes, labels=mvad.labels)
#>  [>] state coding:
#>        [alphabet]  [label]  [long label]
#>      1  employment  EM       employment
#>      2  FE          FE       further education
#>      3  joblessness HE       higher education
#>      4  school      JL       joblessness
#>      5  training    SC       school
#>      6  NA          TR       training
#>  [>] 712 sequences in the data set
#>  [>] min/max sequence length: 23/23

# index plot with rotated labels and smaller font size
seqiplot(mvad.seq, 
         xtlab = attributes(mvad.seq)$names, # label every position
         cex.axis = .4, ylas = 1)


# alternative approach using ggseqplot (wich uses ggplot internally)
# Option 1: labels in two lines
ggseqiplot(mvad.seq[1:10,],
           border = T) +
  scale_x_discrete(labels = attributes(mvad.seq)$names,
                   guide = guide_axis(n.dodge=2)) 
#> Scale for x is already present.
#> Adding another scale for x, which will replace the existing scale.


# Option2: labels in one line but with smaller font size
ggseqiplot(mvad.seq[1:10,],
           border = T) +
  scale_x_discrete(labels = attributes(mvad.seq)$names) +
  theme(axis.text.x = element_text(size = 5))
#> Scale for x is already present.
#> Adding another scale for x, which will replace the existing scale.

Created on 2023-10-17 with reprex v2.0.2