2013-01-14 135 views
4

對於下面給出的例子,我怎麼能畫出虛線之間的區域?R plot中的邊界

下面是代碼:

X <- rnorm(100) 
Y<- X + rnorm(100) 
reg<-lm(Y~X) 

CI <- predict(reg,interval="confidence") 
F <- CI[,1] 
    L <- CI[,2] 
U <- CI[,3] 

plot(X,F) 
abline(reg, col="red") 
reg2<-lm(L~X) 
abline(reg2, col="red", lty="dashed") 
reg3<-lm(U~X) 
abline(reg3, col="red", lty="dashed") 

這裏是輸出

enter image description here

+4

確切的重複,我相信。嘗試'?多邊形' – baptiste

回答

3

好像它應該是一個重複的,但在一個SO搜索我找不到它。我知道這在rhelp上出現了很多次。但有一個財富爲:

require(fortunes) 
fortune("every question") 

Lastly, by what we could call the 'Liaw-Baron principle', every question that can be 
asked has in fact already been asked. 
    -- Dirk Eddelbuettel (citing Andy Liaw's and Jonathan Baron's opinion on unique 
      questions on R-help) 
     R-help (January 2006) 

polygon(x= c(X[order(X)], rev(X[order(X)])), # both x and y go "out" in order; 
      y=c(L[order(X)], rev(U[order(X)])), # and then come "back in reverse order 
      col=rgb(1,0,0,0.3)) # transparent pink. 

> plot(X,Y, pch="+") 
> abline(reg, col="red") 
> polygon(x= c(X\[order(X)\], rev(X\[order(X)\])), y=c(L\[order(X)\],rev(U\[order(X)\])), col=rgb(1,0,0,0.3)) 

enter image description here