سؤال

I'm having trouble adding some text to an plot of time series data in R using xts. I've produced a simple example of the problem.

My text() command seems to do nothing, whereas I can add a points to the plot. I've tried to keep the code simple by using defaults where possible

require(quantmod)

# fetch the data and plot it using default options
getSymbols('MKS.L')
plot(MKS.L$MKS.L.Close)

# try to add text - doesn't appear
text(as.Date('2012-01-01'),y=500,"wobble", cex=4)

# add a point - this does appear
testPos <- xts(600, as.Date('2012-01-01'))
points( testPos, pch = 3, cex = 4, col = "red" )

Any help appreciated - I'm pretty new to R and I've spent hours on this!

هل كانت مفيدة؟

المحلول

Not a direct answer, but the plot.xts function that comes with the xts package is not fully developed.

You're much better off using plot.zoo or plot.xts from the xtsExtra package (which was written as a Google Summer of Code project with the intention being to roll it into the xts package)

Either of these will work:

plot(as.zoo(MKS.L$MKS.L.Close))
text(as.Date('2012-01-01'),y=500,"wobble", cex=4)

#install.packages("xtsExtra", repos="http://r-forge.r-project.org")
xtsExtra::plot.xts(MKS.L$MKS.L.Close)
text(as.Date('2012-01-01'),y=500,"wobble", cex=4)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top