I am trying to use "by" of "for" in order to create many subgraphs using one or two group variables. Both group variables are a factor variables (sex is a dummy and father's social status has multiple levels). How can I add the level (aka the name) of the group in the legend or the title of the graphs?
This is the code I am using.
library(TraMineR)
library(Hmisc)
data(biofam)
biofam.lab <- c("Parent", "Left", "Married", "Left+Marr",
"Child", "Left+Child", "Left+Marr+Child", "Divorced")
biofam.seq <- seqdef(biofam, 10:25, labels=biofam.lab)
class(biofam$sex)
levels(biofam$sex)
describe(biofam$sex)
class(biofam$cspfaj)
levels(biofam$cspfaj)
describe(biofam$cspfaj)
### Simple plots
seqdplot(biofam.seq)
seqdplot(biofam.seq, group=biofam$sex, title="Marital status by gender")
### Plot with automatic title using "by"
by(biofam.seq, biofam$sex, function(X) seqdplot(X, title="X$sex[1]"))
by(biofam.seq, biofam$sex, function(X) seqdplot(X, title=X$sex[1]))
### Plot with automatic title and multiple-grouping using "for"
for(n in c(1, 2, 3)) {
seqdplot(subset(biofam.seq, subset=biofam$cspfaj==(n)), title="(n)")
}
for(n in c(1, 2, 3)) {
seqdplot(subset(biofam.seq, subset=biofam$cspfaj==(n)), group=biofam$sex, title="(n)")
}
The seqdplot function can do it automatically for you using the group argument:
seqdplot(seqobject, group=data.frame$group)
Where seqobject is an object created with the seqdef function.