在条形图中添加平滑拟合线(黄土)

时间:2017-07-29 05:55:52

标签: bar-chart loess

我正在尝试将黄土曲线添加到条形图中。 加载ggplots2包后,我首先创建条形图:

test<-read.csv("tseries.csv", header = TRUE)  
barplot(test$tn90p, beside = TRUE,ylim =c(-6,6))  
lo<-loess(tn10p~year, test)   pred<-predict(lo, se = TRUE)
a<-order(test$year)  

到目前为止一直很好,直到我尝试添加平滑曲线:

lines(test$year[a], pred$fit[a], col = "red", lwd = 2)  

当我这样做时,没有错误消息,提示返回,但没有添加任何行 我错过了什么?
谢谢你的帮助。

注意:当我输入(测试)时,我有

structure(list(year = 1951:1980, tn90p = c(3.126667391, 4.091391006, 
3.11420404, 5.117428018, 2.281128013, 2.654342884, 4.189742845, 
-0.448909654, 1.634574903, -1.324893538, -0.675205784, -1.876889174, 
-2.689793785, 0.364812684, -1.859920287, -1.736813462, -1.527857975, 
-3.214404324, -4.189742845, 0.448909654, -1.634574903, 1.324893538, 
0.675205784, 1.876889174, -0.436873606, -4.45620369, -1.254283753, 
-3.380614556, -0.753270038, 0.560061439)), .Names = c("year", 
"tn90p"), class = "data.frame", row.names = c(NA, -30L))

1 个答案:

答案 0 :(得分:0)

最后我可以让它发挥作用:

  

test&lt; -read.csv(“tseries.csv”,header = TRUE)
  附(测试)
  p = barplot(test $ tn90p,names.arg = test $ year,旁边= TRUE,ylim = c(-6,6))
  LO&LT; -loess(tn90p〜年)
  线(p,预测(lo),col =“红色”,lwd = 3)
  abline(h = 0,lwd = 1)