如何在R中添加平均散点图?

时间:2012-10-30 19:59:27

标签: r regression scatter-plot mean

我的表格中有数据:

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)在图中添加了基于平均序列的回归线。

1 个答案:

答案 0 :(得分: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))