0

Is there a way to add the value of the right most observation on an XTS plot? Another word, I want to display the most recent observation on the chart somehow.

Preferably a number on the axis? or Maybe a horizontal level line.

FXQuantTrader
  • 6,821
  • 3
  • 36
  • 67
user1234440
  • 22,521
  • 18
  • 61
  • 103
  • 1
    Please read [how to provide minimal reproducible examples in R](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#answer-5963610). Then edit & improve it accordingly. – lukeA Oct 08 '16 at 20:21
  • 1
    To display the number, you can use `text`, for a horizontal line, `abline`. Do you have troubles figuring out the right coordinates to pass to these functions? Then please elaborate. – Karsten W. Oct 08 '16 at 21:10

1 Answers1

0

You could show the latest value in the title of the plot (top left corner), and then mark a line on the xts chart for it:

# If you don't already have it, use latest version of xts for nicer plot.xts, which this code uses:
# devtools::install_github("joshuaulrich/xts")
# Sample data:
library(quantmod)
getSymbols("AAPL")
data <- AAPL["2016"]

# Solve the question:
lastval <- last(Cl(data))
plot(Cl(data), main = paste("latest value = ", round(lastval, 2)))
lines(xts(rep(lastval, NROW(data)), index(data)), on = 1, col = "red", lty = 2, main = "hi")

# Check we are we plotting the xts last value? (right most value)
> last(Cl(data))
AAPL.Close
2016-10-07     114.06

enter image description here

Quick and dirty approach: Are you aware that you could plot with package quantmod, and by default they show the latest values "out of the box", which might be quicker/easier for you? Use functions chartSeries and you could use addTA. They show the latest values in the top left corners of the charts. For example:

chartSeries(Cl(AAPL["2016"]))
addTA(RSI(Cl(AAPL["2016"])))

enter image description here

FXQuantTrader
  • 6,821
  • 3
  • 36
  • 67