1

I am new to using TramineR and I just cannot seem to figure out how to arrange the legend in any of the plot types. The legend keeps being cut off by the plots. I have tried to use the seqlegend command but I do not understand how I would integrate the separate legend into my plots.

dat <- data.frame(matrix(sample(0:13, 300 * 26, replace = TRUE), ncol = 26))
dat$age50 <- sample(c("yes", "no"), 300, replace = TRUE)

# Create state sequence object
cci.seq <- seqdef(dat, 
                  var = 1:26, 
                  alphabet = c("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "99"))

# Recode the sequence object
cci.new <- seqrecode(cci.seq, 
                     recodes = list("0 CCI" = "0", 
                                    "1-2 CCI" = c("1", "2"), 
                                    "3-4 CCI" = c("3", "4"), 
                                    "5-6 CCI" = c("5", "6"),
                                    "7-8 CCI" = c("7", "8"), 
                                    "10+ CCI" = c("9", "10", "11", "12", "13"), 
                                    "death" = c("99")))

# Define color palette
cpal(cci.new) <- c("#CAE1EE", "#0772b4", "#f2c673", "#df3337", "#f29862", "#2fa791", "#8A2BE2")

# Basic plot
seqIplot(cci.new, 
         with.legend = TRUE,
         bty = "n",
         idxs = 1:300,
         group = dat$age50,
         legend.prop = 0.1,
ncol=2,
         main = "300 individual sequences by disability status")

I have tried adjusting the cex.legend but it makes the text too small to read.

3
  • 1
    Welcome to Stack Overflow! Please remember that Stack Overflow is not your favourite R forum, but rather a question and answer site for all programming related questions. Thus, always include the tag of the language you are programming in, that way other users familiar with that language can more easily find your question. Take the tour and read up on How to Ask to get more information on how this site works, then edit the question with the relevant tags.
    – Adriaan
    Commented Jun 28 at 13:40
  • 1
    It's easier to help you if you include a simple reproducible example with sample input and desired output that can be used to test and verify possible solutions.
    – MrFlick
    Commented Jun 28 at 13:59
  • Thank you for the input! I have updated accordingly with an example code Commented Jul 1 at 8:29

2 Answers 2

0

If there is not enough space on the canvas for your legend, you could consider changing the size of your plot by calling dev.new(). For example, by typing

dev.new(width = 14, height = 8, unit="in")

Note that you have to add noRStudioGD = TRUE if you are working in RStudio.

If you want to save your plot and not just look at it in an interactive R session, you could test different size specifications until you find one that fits the legend. I personally prefer saving base R plots as pdf (and convert them to png with pdftools::pdf_convert() if necessary):

pdf("iplot.pdf",
    width = 14, height = 8)

seqIplot(cci.new, 
         with.legend = TRUE,
         bty = "n",
         group = dat$age50,
         legend.prop = 0.1,
         ncol=4,
         main = "300 individual sequences by disability status")

dev.off()

Alternatively, you could change to {ggplot2} plot environment using the package {ggseqplot}.

library(ggseqplot)

ggseqiplot(cci.new, 
           group = dat$age50) +
  labs(title = "300 individual sequences by disability status")
0

Several arguments of the seqplot family functions of TraMineR such as cex.legend, legend.prop can be used to control the legend. In addition, most arguments of legend such as ncol can be passed among the ... argument list.

I illustrate with the actcal data:

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)

seqdplot(actcal.seq, group=actcal$sex, ncol=4, legend.prop=.1, cex.legend=1.2)

enter image description here


Edit

For the example you have added in your question, you should increase the proportion of the plot (legend.prop) left for the legend. For example, the following seems to work fine:

seqIplot(cci.new, 
         with.legend = TRUE,
         bty = "n",
         idxs = 1:300,
         group = dat$age50,
         legend.prop = 0.2,
         cex.legend=1.2,
         ncol=4,
         main = "300 individual sequences by disability status")

enter image description here

2
  • Thank you so much for your answer! I have updated my original question with a code example as I am still facing the same problem. Thanks to your help I can manage the legend slightly more but it still does not fit in a reasonable way. It is either too small or cut off. Commented Jul 1 at 8:35
  • I have edited the answer using the working example added to your question
    – Gilbert
    Commented Jul 1 at 13:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.