简体   繁体   中英

Using “by” for creating multiple graph titles

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)")
}

您可以通过将data.frame子集作为function(X)传递来调整函数的参数:

by(data.frame, data.frame$group, function(X) seqdplot(X, title=X$group[1]))

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM