3

我正在与 TraMineR 合作对教育数据进行序列分析。我可以让 R 使用类似于以下的代码生成数据中最常见的 10 个序列的图:

library(TraMineR)

##Loading the data
data(actcal)

##Creating the labels and defining the sequence object
actcal.lab <- c("> 37 hours", "19-36 hours", "1-18 hours", "no work")
actcal.seq <- seqdef(actcal, 13:24, labels=actcal.lab)

## 10 most frequent sequences in the data
actcal.freq <- seqtab(actcal.seq)
actcal.freq

## Plotting the object
seqfplot(actcal.seq, pbarw=FALSE, yaxis="pct", tlim=10:1, cex.legend=.75, withlegend="right")

但是,我还希望在图的右侧有每个序列的频率(在对象 actcal.freq 中)。例如,由上面的代码创建的图中的第一个序列代表 37.9% 的数据(如当前图所示)。根据seqtab,这是 757 个科目。我希望数字 757 出现在右侧 y 轴上(其他序列以此类推)。

这可能吗?我玩过axis(side=4, ...)但从未能够让它重现左 y 轴的间距。

4

1 回答 1

4

好的。这有点乱,但是par如果您默认包含图例,该功能会重置设置,因此您需要将其关闭。然后您可以更轻松地设置轴,然后我们可以返回图例。这应该适用于您上面的测试数据。

#add padding to the right for axis and legend
par("mar"=c(5,4,4,8)+.1)

#plot w/o axis
seqfplot(actcal.seq, pbarw=FALSE, yaxis="pct", tlim=10:1, withlegend=F)

#plot right axis with freqs
axis(4, at = seq(.7, by=1.2, length.out=length(attr(actcal.freq,"freq")$Freq)), 
    labels = rev(attr(actcal.freq,"freq")$Freq), 
    mgp = c(1.5, 0.5, 0), las = 1, tick = FALSE)

#now put the legend on        
legend("right", legend=attr(actcal.seq, "labels"), 
    fill=attr(actcal.seq, "cpal"), 
    inset=-.3, bty="o", xpd=NA, cex=.75)

您可能需要稍微调整一下边距,尤其是 的inset=参数legend以使其正确放置。我希望您的真实数据与此没有太大不同,因为您确实必须深入研究该函数以查看它如何进行格式化以使事物匹配。

右侧 y 轴的 TraMineR 图

于 2014-05-29T01:26:13.103 回答