在R中绘制平均得分

时间:2014-07-01 13:23:22

标签: r plot

这个问题与我之前提到的question有关。

所以我得到了一些代码,其中我有人的整体平均情绪(虚线),每日平均情绪(水平灰线)和每次测量的情绪(红线)。我需要添加一行连接每天的手段。所以在这种情况下,这意味着我必须在两条灰线之间绘制一条线。我在下面添加了一张图片,其中绿线表示我想要添加的行类型。

enter image description here

MWE:

beeps.MWE <- c(91.188697, 87.846194, 93.166418, 96.249094, 95.495146, 99.362597, 94.373646, 
81.995712, 87.626009, 91.880172, 93.112647, 99.349234, 87.073372, 85.161982, 88.119728, 
89.738318, 68.891181, 62.504569, 75.131526, 56.035989, 66.035109, 56.012537)

day.MWE <- rep(c(91.35869, 63.17620), each = 11)

loc.MWE <- c(8, 15)

plot(day.MWE, type = "n", pch = 15, cex = 1.5, ylim = c(40, 110), bty = "n", 
ylab = "score on PA/NA", xlab = "days of person i", axes = FALSE)
dayUn <- unique(day.MWE)
for (i in seq_along(dayUn))
{
  lines(which(day.MWE==dayUn[i]),day.MWE[day.MWE==dayUn[i]], lwd = "2", col = "grey")
  lines(which(day.MWE==dayUn[i]),day.MWE[day.MWE==dayUn[i]], lwd = "2")
}
lines(1:length(beeps.MWE), rep(mean(day.MWE), 22), lwd = "2", lty = 2)
axis(1, at = c(1, 20), labels = c("day 1", "day 2"))
axis(2, las = 1)

这是上述代码的输出:

enter image description here

2 个答案:

答案 0 :(得分:1)

您可以像使用lines将任何其他lines添加到地图一样:

lines(c(mean(which(day.MWE==dayUn[1])),mean(which(day.MWE==dayUn[2]))),dayUn,col="green",lwd=5)

答案 1 :(得分:1)

如果你有一个未知(或大)的天数,你可以用几种方式来做,但一个简单的方法是添加

if(i>1){
    lines(c(mean(which(day.MWE==dayUn[i-1])),mean(which(day.MWE==dayUn[i]))),dayUn[(i-1):i],col="green",lwd=5)
}
在for循环中

。这将从日 i-1 到日 i 画一条线 i = 2,3,...