5

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)")
}
apaderno
  • 28,547
  • 16
  • 75
  • 90
  • 1
    Please post code that can be cut-and-pasted and will provide an example to work with. There is no data object called data.frame at that page. – IRTFM Dec 07 '12 at 02:25
  • Maybe, my example code makes my question clearer, now. –  Dec 08 '12 at 02:37
  • Yes, it's more clear, although I get an error with the `seqdplot` call within the for loop. (A subscript out of bounds eeror ... is that wha tyou are seeing?) – IRTFM Dec 08 '12 at 03:24
  • If you use a subset of the sequences, you should also use a subset of the group covariate (argument group=biofam$sex). – Matthias Studer Dec 10 '12 at 11:39

2 Answers2

2

You can adjust the arguments of a function by passing the data.frame subset as function(X):

by(data.frame, data.frame$group, function(X) seqdplot(X, title=X$group[1]))
Señor O
  • 17,049
  • 2
  • 45
  • 47
  • The title seems to need a string input. How can I enter "X$group[1]" as a string while still using the level of the variable? –  Dec 08 '12 at 02:56
  • It appears you are using two different objects in `by`. Keep in mind only the first object gets passed to `function(X)` – Señor O Dec 08 '12 at 03:22
2

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.

Matthias Studer
  • 1,722
  • 1
  • 10
  • 24
  • Very nice feature indeed, but I want to use two (or more) grouping variables, therefore I want to combine it with "by" or "for" in order to produce new graphs for each new level of the second grouping variable. –  Dec 08 '12 at 02:59
  • If you want multiple grouping variable, you can use the "interaction" function: seqdplot(seqobject, group=interaction(data.frame$group1, data.frame$group2, ...)) – Matthias Studer Dec 08 '12 at 09:13