1

我有以下形式的数据:

Num Percent  
5   9.44  
4   19.7  
4   10.64  
4   10.81  
4   10.97  
4   11.35  
3   13.18  
3   24.54  
3   24.99  
3   12.95  
3   13.17  
3   24.29  
3   13.81  
3   13.93  
3   27.87  
3   13.26  
2   41.41  
2   34.19  
2   41.6  
2   19.31  
2   34.4  
2   38.72  
2   18.16  
2   36.28  
2   18.95  
2   18.49

我想绘制数据,以便与'num'(x)和'percent'(y)的图一起,将x轴(2,3,4,5)每个点的平均值添加到绘制了图和基于平均系列的回归线。

4

1 回答 1

1

调用您的数据框DF

MeansByNum <- tapply(DF$Percent, DF$Num, mean) #Create an array of means by Num
NewDF <- data.frame(cbind(Num = as.numeric(as.vector(names(MeansByNum))),
                          Percent = as.numeric(MeansByNum)))
plot(Percent~Num, DF)
points(NewDF$Num, NewDF$Percent, col="red")
abline(lm(Percent~Num, NewDF))
于 2012-10-30T20:15:01.113 回答