如何在 traminer 中结合状态分布图和单独的图例?

How to combine state distribution plot and separate legend in traminer?

在 TraMineR 中使用 seqdplot 绘制多个集群可能会使图例变得混乱,尤其是与众多状态结合使用时。这需要额外的选项来修改图例,这些选项可用于函数 seqlegend。但是,我很难将状态分布图 (seqdplot) 与单独的修改图例 (seqlegend) 结合起来。理想情况下,人们想要绘制没有图例的集群(例如 9),然后在可用的右下行中添加单独的图例,但单独的图例会生成一个新图 window。有人可以帮忙吗?

这是一个使用 biofam 数据的示例。使用我在自己的研究中使用的数据,图例变得更加混乱,因为我有 11 个州。

#Data
library(TraMineR)
library(WeightedCluster)
data(biofam)
biofam.seq <- seqdef(biofam[501:600, 10:25])

#OM distances
biofam.om <- seqdist(biofam.seq, method = "OM", indel = 3, sm = "TRATE")

#9 clusters
wardCluster <- hclust(as.dist(biofam.om), method = "ward.D2")
cluster9 <- cutree(wardCluster, k = 9)

#State distribution plot
seqdplot(biofam.seq, group = cluster9, with.legend = F)

#Separate legend
seqlegend(biofam.seq, title = "States", ncol = 2)

#Combine state distribution plot and separate legend  
#??

谢谢。

AFAIK seqlegend() 在您绘制的其他图使用 groups 参数时不起作用。在您的情况下,seqlegend() 唯一添加的是标题 "States"。如果您希望添加图例以便自定义图例中的内容等,您可以通过提供分析中使用的相应 alphabetstates 来实现。

该软件包的网站有几个演练和指南,列举了各种选项等等:Link to their webiste

#Data
library(TraMineR)
library(WeightedCluster)
data(biofam)

## Generate alphabet and states
alphabet <- 0:7
states <- letters[seq_along(alphabet)]

biofam.seq <- seqdef(biofam[501:600, 10:25], states = states, alphabet = alphabet)

#OM distances
biofam.om <- seqdist(biofam.seq, method = "OM", indel = 3, sm = "TRATE")

#9 clusters
wardCluster <- hclust(as.dist(biofam.om), method = "ward.D2")
cluster9 <- cutree(wardCluster, k = 9)

#State distribution plot
seqdplot(biofam.seq, group = cluster9, with.legend = TRUE)

seqplot功能不允许控制图例的列数,也不允许添加图例标题。因此,您必须通过为禁用图例的每个组生成单独的图并在之后添加图例来自己编写图。以下是如何做到这一点:

cluster9 <- factor(cluster9)
levc <- levels(cluster9)
lev <- length(levc)

par(mfrow=c(5,2))
for (i in 1:lev)
   seqdplot(biofam.seq[cluster9 == levc[i],], border=NA, main=levc[i], with.legend=FALSE)
seqlegend(biofam.seq, ncol=4, cex = 1.2, title='States')

======================== 更新,2018 年 10 月 1 日 =================

自 TraMineR V 2.0-9 起,seqplot 系列函数现在支持(适用时)参数 ncol 以控制图例中的列数。要为图例添加标题,您仍然必须按上图所示进行操作。