2017-03-16 207 views
2

如何修改此代碼以使其具有一個繪圖,以便它在圖形上顯示 其每個點的相應行號作爲標籤。將標籤*行號*添加到繪圖

inter <- seq(7.5, 21.5, 1) 
LogDifference <- c("na",1.5,0.8,0.6,0.01,-0.57,-0.11,0.41,0.068,-0.19,-0.31,0.05,0.14,0.6,0.5) 
S<-data.frame(inter,LogDifference) 
plot(x = S$inter,S$LogDifference) 

回答

1

首先,請注意,您的基本情節沒有做你想做的事。 正在繪製的y值是數字1到14.我認爲你想要 LogDifference中的數值。您可以通過 首先將LogDifference轉換爲字符(這是一個因子),然後將 轉換爲數字來修復此問題。我只是拋棄了「na」。

之後,您可以使用text將標籤放置在點旁邊。 完整的代碼是:

inter <- seq(7.5, 21.5, 1) 
LogDifference <- c("na",1.5,0.8,0.6,0.01,-0.57,-0.11,0.41,0.068, 
    -0.19,-0.31,0.05,0.14,0.6,0.5) 
S<-data.frame(inter,LogDifference) 
plot(x = S$inter[-1], as.numeric(as.character(S$LogDifference[-1]))) 
text(x=inter[-1]+0.4, y=as.numeric(as.character(LogDifference[-1]))+0.05, labels=2:15) 

Labeled graph