2011-09-05 28 views
4

我想Adding Points, Legends and Text to plots using xts objects將有這個問題的答案,但顯然不是...加分XTS情節

require(quantmod) 
getSymbols("SAM") 
big.red.dot <- zoo(85, as.Date("2011-05-05")) 
plot(SAM['2011']) 
points( big.red.dot, col="red", pch=19, cex=5 ) 

這一次似乎是直接從教科書。儘管如此,?plot.zoo不包含point()的任何示例。

+0

@Joshua烏爾裏希感謝您糾正稱號。標題編輯順序清楚地表明,如果最後一個存在,我不理解'plot.zoo','plot.xts'和''quantmod' plot「之間的區別。你能指點我一個參考嗎? – isomorphismes

+1

@Lau Tzu:'plot'是一個通用函數。 'plot.zoo'和'plot.xts'是動物園和xts類對象的簡單方法。參見'?plot.zoo'和'?plot.xts'來獲取每個函數的描述。 quantmod具有'chartSeries'繪圖功能。 –

+0

@isomorphismes我應該看看'Methods(plot)'以及瞭解'plot'(通用函數)的工作原理。另外http://adv-r.had.co.nz/OO-essentials.html#s3 – isomorphismes

回答

8

默認情況下,由quantmod::getSymbols創建的對象實際上是xts類。這意味着你的big.red.dot應該是一個xts對象:(!和寫作包)

big.red.dot <- xts(85, as.Date("2011-05-05")) 
plot(SAM['2011']) 
points( big.red.dot, col="red", pch=19, cex=5 ) 

enter image description here

+0

謝謝!答案非常明顯,我並不感到驚訝。 – isomorphismes