gpt4 book ai didi

r - 如何使用序列频率将第二个 y 轴添加到 seqfplot?

转载 作者:行者123 更新时间:2023-12-01 12:41:43 25 4
gpt4 key购买 nike

我正在使用 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 轴的间距。

最佳答案

好的。这有点乱,但是如果您默认包含图例,该函数会重置 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)

您可能需要稍微调整一下边距,尤其是 legendinset= 参数,以使其正确放置。我希望您的真实数据与此没有太大差异,因为您真的必须深入了解该函数,看看它如何进行格式化以使内容匹配。

TraMineR plot with right y-axis

关于r - 如何使用序列频率将第二个 y 轴添加到 seqfplot?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23920494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com