xts_heatmap <- function(x){
data.frame(Date=as.Date(index(x)), x[,1]) %>%
setNames(c("Date","Value")) %>%
dplyr::mutate(
Year=lubridate::year(Date),
Month=lubridate::month(Date),
# I use factors here to get plot ordering in the right order
# without worrying about locale
MonthTag=factor(Month,levels=as.character(1:12),
labels=c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"),ordered=TRUE),
# week start on Monday in my world
Wday=lubridate::wday(Date,week_start=1),
# the rev reverse here is just for the plotting order
WdayTag=factor(Wday,levels=rev(1:7),labels=rev(c("Mon","Tue","Wed","Thu","Fri","Sat","Sun")),ordered=TRUE),
Week=as.numeric(format(Date,"%W"))
) %>%
# ok here we group by year and month and then calculate the week of the month
# we are currently in
dplyr::group_by(Year,Month) %>%
dplyr::mutate(Wmonth=1+Week-min(Week)) %>%
dplyr::ungroup() %>%
ggplot(aes(x=Wmonth, y=WdayTag, fill = Value)) +
geom_tile(colour = "white") +
facet_grid(Year~MonthTag) +
scale_fill_gradient(low="red", high="yellow") +
labs(x="Week of Month", y=NULL)
}
Download some Data, e.g. the CBOE VIX
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
##
## This message is shown once per session and may be disabled by setting
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
## [1] "^VIX"
## VIX.Close
## 2007-01-03 12.04
## 2007-01-04 11.51
## 2007-01-05 12.14
## 2007-01-08 12.00
## 2007-01-09 11.91
## 2007-01-10 11.47