TraminR sequence plosts

59 Views Asked by At

I'm trying to use ggplot to create sequence plots, for the sake of keeping the same visual style within my paper using sequence analysis

As I have 8 seqiplot plot I would like to make it into one using this code

plot.data<- data.frame("v1"=rnorm(20), "v2"=rnorm(20))

blank<- rectGrob(gp=gpar(fill="white", col="white", fontfamily = "Times", face = "bold"))


title1 <- grobTree(rectGrob(gp=gpar(fill="white", col="white",lwd=5)),
                   textGrob("Employed", gp=gpar(col="black", fontsize= 20)))

title2 <- grobTree(rectGrob(gp=gpar(fill="white", col="white", lwd=5)),
                   textGrob("Unemployed", gp=gpar(col="black", fontsize= 20)))

title3 <- grobTree(rectGrob(gp=gpar(fill="white", col="white", lwd=5)),
                   textGrob("1974", rot = 90, gp=gpar(col="black", fontsize= 20)))

title4 <- grobTree(rectGrob(gp=gpar(fill="white",  col="white", lwd=5)),
                   textGrob("2000", rot = 90, gp=gpar(col="black", fontsize= 20)))

title5 <- grobTree(rectGrob(gp=gpar(fill="white", col="white", lwd=5)),
                   textGrob("2005", rot = 90, gp=gpar(col="black", fontsize= 20)))

title6 <- grobTree(rectGrob(gp=gpar(fill="white",  col="white", lwd=5)),
                   textGrob("2014", rot = 90, gp=gpar(col="black", fontsize= 20)))


ggarrange(ggarrange(blank,   title1, title2,  ncol=3, widths = c(1, 5, 5)),
          ggarrange(title3, Emp_1974, Unemp_1974, title4, Emp_2000, Unemp_2000, title5, Emp_2005, Unemp_2005, title6, Emp_2014, Unemp_2014, ncol=3, nrow=4, widths = c(1, 5, 5)),
          common.legend= T, 

legend = "bottom", nrow=2, heights= c(1, 10))

I received the following error

Warning messages:
1: In as_grob.default(plot) :
  Cannot convert object of class list into a grob.
2: In as_grob.default(plot) :
  Cannot convert object of class list into a grob.
3: In as_grob.default(plot) :
  Cannot convert object of class list into a grob.
4: In as_grob.default(plot) :
  Cannot convert object of class list into a grob.
5: In as_grob.default(plot) :
  Cannot convert object of class list into a grob.
6: In as_grob.default(plot) :
  Cannot convert object of class list into a grob.
7: In as_grob.default(plot) :
  Cannot convert object of class list into a grob.
8: In as_grob.default(plot) :
  Cannot convert object of class list into a grob.

Data

dput(Emp_1974)
list(xlog = FALSE, ylog = FALSE, adj = 0.5, ann = TRUE, ask = FALSE, 
    bg = "transparent", bty = "o", cex = 1, cex.axis = 1, cex.lab = 1, 
    cex.main = 1.2, cex.sub = 1, col = "black", col.axis = "black", 
    col.lab = "black", col.main = "black", col.sub = "black", 
    crt = 0, err = 0L, family = "", fg = "black", fig = c(0, 
    1, 0, 0.15), fin = c(10.541665625, 7.885415625), font = 1L, 
    font.axis = 1L, font.lab = 1L, font.main = 2L, font.sub = 1L, 
    lab = c(5L, 5L, 7L), las = 0L, lend = "round", lheight = 1, 
    ljoin = "round", lmitre = 10, lty = "solid", lwd = 1, mai = c(1.02, 
    0.82, 0.82, 0.42), mar = c(5.1, 4.1, 4.1, 2.1), mex = 1, 
    mfcol = c(1L, 1L), mfg = c(1L, 1L, 1L, 1L), mfrow = c(1L, 
    1L), mgp = c(3, 1, 0), mkh = 0.001, new = TRUE, oma = c(0, 
    0, 0, 0), omd = c(0, 1, 0, 1), omi = c(0, 0, 0, 0), pch = 1L, 
    pin = c(9.301665625, 6.045415625), plt = c(0.0588142350606952, 
    0.941185764939305, 0.116671085425507, 0.883328914574493), 
    ps = 12L, pty = "m", smo = 1, srt = 0, tck = NA_real_, tcl = -0.5, 
    usr = c(0.568, 1.432, -1.08, 1.08), xaxp = c(0, 1, 5), xaxs = "r", 
    xaxt = "s", xpd = FALSE, yaxp = c(0, 1, 5), yaxs = "r", yaxt = "s", 
    ylbias = 0.2)
 dput(Unemp_1974)
list(xlog = FALSE, ylog = FALSE, adj = 0.5, ann = TRUE, ask = FALSE, 
    bg = "transparent", bty = "o", cex = 1, cex.axis = 1, cex.lab = 1, 
    cex.main = 1.2, cex.sub = 1, col = "black", col.axis = "black", 
    col.lab = "black", col.main = "black", col.sub = "black", 
    crt = 0, err = 0L, family = "", fg = "black", fig = c(0, 
    1, 0, 0.15), fin = c(10.541665625, 7.885415625), font = 1L, 
    font.axis = 1L, font.lab = 1L, font.main = 2L, font.sub = 1L, 
    lab = c(5L, 5L, 7L), las = 0L, lend = "round", lheight = 1, 
    ljoin = "round", lmitre = 10, lty = "solid", lwd = 1, mai = c(1.02, 
    0.82, 0.82, 0.42), mar = c(5.1, 4.1, 4.1, 2.1), mex = 1, 
    mfcol = c(1L, 1L), mfg = c(1L, 1L, 1L, 1L), mfrow = c(1L, 
    1L), mgp = c(3, 1, 0), mkh = 0.001, new = TRUE, oma = c(0, 
    0, 0, 0), omd = c(0, 1, 0, 1), omi = c(0, 0, 0, 0), pch = 1L, 
    pin = c(9.301665625, 6.045415625), plt = c(0.0588142350606952, 
    0.941185764939305, 0.116671085425507, 0.883328914574493), 
    ps = 12L, pty = "m", smo = 1, srt = 0, tck = NA_real_, tcl = -0.5, 
    usr = c(0.568, 1.432, -1.08, 1.08), xaxp = c(0, 1, 5), xaxs = "r", 
    xaxt = "s", xpd = FALSE, yaxp = c(0, 1, 5), yaxs = "r", yaxt = "s", 
    ylbias = 0.2)


dput(Emp_2000)
list(xlog = FALSE, ylog = FALSE, adj = 0.5, ann = TRUE, ask = FALSE, 
    bg = "transparent", bty = "o", cex = 1, cex.axis = 1, cex.lab = 1, 
    cex.main = 1.2, cex.sub = 1, col = "black", col.axis = "black", 
    col.lab = "black", col.main = "black", col.sub = "black", 
    crt = 0, err = 0L, family = "", fg = "black", fig = c(0, 
    1, 0, 0.15), fin = c(10.541665625, 7.885415625), font = 1L, 
    font.axis = 1L, font.lab = 1L, font.main = 2L, font.sub = 1L, 
    lab = c(5L, 5L, 7L), las = 0L, lend = "round", lheight = 1, 
    ljoin = "round", lmitre = 10, lty = "solid", lwd = 1, mai = c(1.02, 
    0.82, 0.82, 0.42), mar = c(5.1, 4.1, 4.1, 2.1), mex = 1, 
    mfcol = c(1L, 1L), mfg = c(1L, 1L, 1L, 1L), mfrow = c(1L, 
    1L), mgp = c(3, 1, 0), mkh = 0.001, new = TRUE, oma = c(0, 
    0, 0, 0), omd = c(0, 1, 0, 1), omi = c(0, 0, 0, 0), pch = 1L, 
    pin = c(9.301665625, 6.045415625), plt = c(0.0588142350606952, 
    0.941185764939305, 0.116671085425507, 0.883328914574493), 
    ps = 12L, pty = "m", smo = 1, srt = 0, tck = NA_real_, tcl = -0.5, 
    usr = c(0.568, 1.432, -1.08, 1.08), xaxp = c(0, 1, 5), xaxs = "r", 
    xaxt = "s", xpd = FALSE, yaxp = c(0, 1, 5), yaxs = "r", yaxt = "s", 
    ylbias = 0.2)

dput(Unemp_2000)
list(xlog = FALSE, ylog = FALSE, adj = 0.5, ann = TRUE, ask = FALSE, 
    bg = "transparent", bty = "o", cex = 1, cex.axis = 1, cex.lab = 1, 
    cex.main = 1.2, cex.sub = 1, col = "black", col.axis = "black", 
    col.lab = "black", col.main = "black", col.sub = "black", 
    crt = 0, err = 0L, family = "", fg = "black", fig = c(0, 
    1, 0, 0.15), fin = c(10.541665625, 7.885415625), font = 1L, 
    font.axis = 1L, font.lab = 1L, font.main = 2L, font.sub = 1L, 
    lab = c(5L, 5L, 7L), las = 0L, lend = "round", lheight = 1, 
    ljoin = "round", lmitre = 10, lty = "solid", lwd = 1, mai = c(1.02, 
    0.82, 0.82, 0.42), mar = c(5.1, 4.1, 4.1, 2.1), mex = 1, 
    mfcol = c(1L, 1L), mfg = c(1L, 1L, 1L, 1L), mfrow = c(1L, 
    1L), mgp = c(3, 1, 0), mkh = 0.001, new = TRUE, oma = c(0, 
    0, 0, 0), omd = c(0, 1, 0, 1), omi = c(0, 0, 0, 0), pch = 1L, 
    pin = c(9.301665625, 6.045415625), plt = c(0.0588142350606952, 
    0.941185764939305, 0.116671085425507, 0.883328914574493), 
    ps = 12L, pty = "m", smo = 1, srt = 0, tck = NA_real_, tcl = -0.5, 
    usr = c(0.568, 1.432, -1.08, 1.08), xaxp = c(0, 1, 5), xaxs = "r", 
    xaxt = "s", xpd = FALSE, yaxp = c(0, 1, 5), yaxs = "r", yaxt = "s", 
    ylbias = 0.2)
dput(Emp_2005)
list(xlog = FALSE, ylog = FALSE, adj = 0.5, ann = TRUE, ask = FALSE, 
    bg = "transparent", bty = "o", cex = 1, cex.axis = 1, cex.lab = 1, 
    cex.main = 1.2, cex.sub = 1, col = "black", col.axis = "black", 
    col.lab = "black", col.main = "black", col.sub = "black", 
    crt = 0, err = 0L, family = "", fg = "black", fig = c(0, 
    1, 0, 0.15), fin = c(10.541665625, 7.885415625), font = 1L, 
    font.axis = 1L, font.lab = 1L, font.main = 2L, font.sub = 1L, 
    lab = c(5L, 5L, 7L), las = 0L, lend = "round", lheight = 1, 
    ljoin = "round", lmitre = 10, lty = "solid", lwd = 1, mai = c(1.02, 
    0.82, 0.82, 0.42), mar = c(5.1, 4.1, 4.1, 2.1), mex = 1, 
    mfcol = c(1L, 1L), mfg = c(1L, 1L, 1L, 1L), mfrow = c(1L, 
    1L), mgp = c(3, 1, 0), mkh = 0.001, new = TRUE, oma = c(0, 
    0, 0, 0), omd = c(0, 1, 0, 1), omi = c(0, 0, 0, 0), pch = 1L, 
    pin = c(9.301665625, 6.045415625), plt = c(0.0588142350606952, 
    0.941185764939305, 0.116671085425507, 0.883328914574493), 
    ps = 12L, pty = "m", smo = 1, srt = 0, tck = NA_real_, tcl = -0.5, 
    usr = c(0.568, 1.432, -1.08, 1.08), xaxp = c(0, 1, 5), xaxs = "r", 
    xaxt = "s", xpd = FALSE, yaxp = c(0, 1, 5), yaxs = "r", yaxt = "s", 
    ylbias = 0.2)


dput(Unemp_2005)
list(xlog = FALSE, ylog = FALSE, adj = 0.5, ann = TRUE, ask = FALSE, 
    bg = "transparent", bty = "o", cex = 1, cex.axis = 1, cex.lab = 1, 
    cex.main = 1.2, cex.sub = 1, col = "black", col.axis = "black", 
    col.lab = "black", col.main = "black", col.sub = "black", 
    crt = 0, err = 0L, family = "", fg = "black", fig = c(0, 
    1, 0, 0.15), fin = c(10.541665625, 7.885415625), font = 1L, 
    font.axis = 1L, font.lab = 1L, font.main = 2L, font.sub = 1L, 
    lab = c(5L, 5L, 7L), las = 0L, lend = "round", lheight = 1, 
    ljoin = "round", lmitre = 10, lty = "solid", lwd = 1, mai = c(1.02, 
    0.82, 0.82, 0.42), mar = c(5.1, 4.1, 4.1, 2.1), mex = 1, 
    mfcol = c(1L, 1L), mfg = c(1L, 1L, 1L, 1L), mfrow = c(1L, 
    1L), mgp = c(3, 1, 0), mkh = 0.001, new = TRUE, oma = c(0, 
    0, 0, 0), omd = c(0, 1, 0, 1), omi = c(0, 0, 0, 0), pch = 1L, 
    pin = c(9.301665625, 6.045415625), plt = c(0.0588142350606952, 
    0.941185764939305, 0.116671085425507, 0.883328914574493), 
    ps = 12L, pty = "m", smo = 1, srt = 0, tck = NA_real_, tcl = -0.5, 
    usr = c(0.568, 1.432, -1.08, 1.08), xaxp = c(0, 1, 5), xaxs = "r", 
    xaxt = "s", xpd = FALSE, yaxp = c(0, 1, 5), yaxs = "r", yaxt = "s", 
    ylbias = 0.2)

dput(Emp_2014)
list(xlog = FALSE, ylog = FALSE, adj = 0.5, ann = TRUE, ask = FALSE, 
    bg = "transparent", bty = "o", cex = 1, cex.axis = 1, cex.lab = 1, 
    cex.main = 1.2, cex.sub = 1, col = "black", col.axis = "black", 
    col.lab = "black", col.main = "black", col.sub = "black", 
    crt = 0, err = 0L, family = "", fg = "black", fig = c(0, 
    1, 0, 0.15), fin = c(10.541665625, 7.885415625), font = 1L, 
    font.axis = 1L, font.lab = 1L, font.main = 2L, font.sub = 1L, 
    lab = c(5L, 5L, 7L), las = 0L, lend = "round", lheight = 1, 
    ljoin = "round", lmitre = 10, lty = "solid", lwd = 1, mai = c(1.02, 
    0.82, 0.82, 0.42), mar = c(5.1, 4.1, 4.1, 2.1), mex = 1, 
    mfcol = c(1L, 1L), mfg = c(1L, 1L, 1L, 1L), mfrow = c(1L, 
    1L), mgp = c(3, 1, 0), mkh = 0.001, new = TRUE, oma = c(0, 
    0, 0, 0), omd = c(0, 1, 0, 1), omi = c(0, 0, 0, 0), pch = 1L, 
    pin = c(9.301665625, 6.045415625), plt = c(0.0588142350606952, 
    0.941185764939305, 0.116671085425507, 0.883328914574493), 
    ps = 12L, pty = "m", smo = 1, srt = 0, tck = NA_real_, tcl = -0.5, 
    usr = c(0.568, 1.432, -1.08, 1.08), xaxp = c(0, 1, 5), xaxs = "r", 
    xaxt = "s", xpd = FALSE, yaxp = c(0, 1, 5), yaxs = "r", yaxt = "s", 
    ylbias = 0.2)

 dput(Unemp_2014)
list(xlog = FALSE, ylog = FALSE, adj = 0.5, ann = TRUE, ask = FALSE, 
    bg = "transparent", bty = "o", cex = 1, cex.axis = 1, cex.lab = 1, 
    cex.main = 1.2, cex.sub = 1, col = "black", col.axis = "black", 
    col.lab = "black", col.main = "black", col.sub = "black", 
    crt = 0, err = 0L, family = "", fg = "black", fig = c(0, 
    1, 0, 0.15), fin = c(10.541665625, 7.885415625), font = 1L, 
    font.axis = 1L, font.lab = 1L, font.main = 2L, font.sub = 1L, 
    lab = c(5L, 5L, 7L), las = 0L, lend = "round", lheight = 1, 
    ljoin = "round", lmitre = 10, lty = "solid", lwd = 1, mai = c(1.02, 
    0.82, 0.82, 0.42), mar = c(5.1, 4.1, 4.1, 2.1), mex = 1, 
    mfcol = c(1L, 1L), mfg = c(1L, 1L, 1L, 1L), mfrow = c(1L, 
    1L), mgp = c(3, 1, 0), mkh = 0.001, new = TRUE, oma = c(0, 
    0, 0, 0), omd = c(0, 1, 0, 1), omi = c(0, 0, 0, 0), pch = 1L, 
    pin = c(9.301665625, 6.045415625), plt = c(0.0588142350606952, 
    0.941185764939305, 0.116671085425507, 0.883328914574493), 
    ps = 12L, pty = "m", smo = 1, srt = 0, tck = NA_real_, tcl = -0.5, 
    usr = c(0.568, 1.432, -1.08, 1.08), xaxp = c(0, 1, 5), xaxs = "r", 
    xaxt = "s", xpd = FALSE, yaxp = c(0, 1, 5), yaxs = "r", yaxt = "s", 
    ylbias = 0.2)

Plot that I am aiming for with sequences: enter image description here

1

There are 1 best solutions below

0
maraab On

As {TraMineR} uses base R plots, things become a little bit more tedious (but probably still doable) if you compose complex plots using {ggarrange} or {patchwork}, I recommend using {ggseqplot} in scenarios like that. Below you find an example joining several state distribution plots.

For composing such a plot with TraMineR::seqplots, I would turn to R's layout function. You can find an example on the companion website of our sequence analysis book: https://sa-book.github.io/rChapter5-5.html

library(TraMineR)
library(patchwork)

# Example sequence data from the mvad data set
data(mvad)

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)


# generate a group indicator distinguishing 4 groups
groupvar <- as.factor(rep(1:4, length.out = nrow(mvad)))

# group-specific subplot for males
p1 <- ggseqdplot(mvad.seq[mvad$male == "yes",], 
                 group = groupvar[mvad$male == "yes"], facet_ncol = 1) +
  labs(title = "Men")

# group-specific subplot for females
p2 <- ggseqdplot(mvad.seq[mvad$male == "no",], 
                 group = groupvar[mvad$male == "no"], facet_ncol = 1) +
  labs(title = "Women", y=NULL)

# combine the two plots using patchwork
p1 + p2 + plot_layout(guides = "collect") & 
  theme(legend.position = 'bottom',
        plot.title = element_text(hjust = 0.5, 
                                  size = 20,
                                  face = "bold"))

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