I gather the following data. In the plot, I would like to have a heading that says "most recent date: xxxx". (dynamic) Does anyone have a solution? Open for other ideas with similar outcome to show the most recent date on the plot.Thank you!
library(fImport)
library(zoo)
sentiment <- fredSeries("UMCSENT", from = "1995-01-04", to = "2017-01-01")
sp500 <- fredSeries("SP500", from = "1995-01-04", to = "2017-01-01")
plot(sentiment, type = "l", col = "Orange", ylab = "", xaxt = "n",
main = "Uni. Michigan Consumer Sentiment vs. SP500")
axis(2)
par(new = TRUE)
plot(sp500, , type = "l", col = "red", xaxt = "n", yaxt = "n", xlab = "Time", ylab = "")
axis(4)
mtext("SP500", side = 4, line = 3)
legend("bottomleft", col = c("Orange", "red"), lty = 1,
legend = c("Sentiment (Left)", "SP500 (Right)"))
Insert the most recent date (not working): (see comment section)
mtext(paste("Most Recent Data",
index(sentiment$UMCSENT)[dim(sentiment)[1]]), cex = 1, line = 0)
You can put the information in the sub title below the plot:
title(sub = paste("Most Recent Data", index(sentiment$UMCSENT)[dim(sentiment)[1]]))
Update
After re-reading the comments, I think your issue is how to extract the date from your data. I am not familiar with timeSeries
, but would
title(sub = paste("Most Recent Data", tail(rownames(sentiment), 1)))
do the trick? Now you can decide whether you want to use mtext
or title
. This gives you the following plot:
If it is the date you want you need to look at the structure of sentiment
Time Series:
Name: object
Data Matrix:
Dimension: 255 1
Column Names: UMCSENT
Row Names: 1995-02-01 ... 2016-04-01
Positions:
Start: 1995-02-01
End: 2016-04-01
With:
Format: %Y-%m-%d
FinCenter: GMT
Units: UMCSENT
Title: Time Series Object
Documentation: Mon May 23 09:59:47 2016
Here we see that the dates are within in the row.names
of the object.
Thus add the following to your code to get the latest date
title(sub = paste("Most Recent Data", row.names(sentiment)[nrow(sentiment)]))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.