2017-07-29 35 views
0

我正在嘗試將黃土曲線添加到條形圖中。 加載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) 

當我這樣做,沒有錯誤信息,提示符會回來,但不會添加行。
我錯過了什麼?
感謝您的幫助。

注:當我dput(測試),我有這個

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)) 
+0

我意識到我的代碼中存在拼寫錯誤。沒有tn10p。我只有90P。 – Soul

回答

0

最後,我可以讓它工作:

測試< -read.csv(「T系列.sv「,header = TRUE)
attach(test)
p = barplot(test $ tn90p,names.arg = test $ year,beside = TRUE,ylim = c(-6,6))
LO < -loess(tn90p〜年)
線(P,預測(LO),山口= 「紅」,LWD = 3)
abline(H = 0,LWD = 1)