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.
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.
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
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"])))