« Thoughts on the SF Bay Data Mining Camp | Main | RPE: the R Productivity Environment for Windows »

November 03, 2009


Feed You can follow this conversation by subscribing to the comment feed for this post.

Nice thanks! It's amazing how giving the R community is. Thanks for the blog.

Here's a quick rendition in ggplot2:

stock.data <- transform(stock.data,
  week = as.POSIXlt(Date)$yday %/% 7 + 1,
  wday = as.POSIXlt(Date)$wday,
  year = as.POSIXlt(Date)$year + 1900)

ggplot(stock.data, aes(week, wday, fill = Adj.Close)) +
geom_tile(colour = "white") +
scale_fill_gradientn(colours = c("#D61818","#FFAE63","#FFFFBD","#B5E384")) +
facet_wrap(~ year, ncol = 1)

But maybe a better display of the same data is :

qplot(week, Adj.Close, data = stock.data, colour = factor(wday), geom = "line") + 
  facet_wrap(~ year, ncol = 1)

Your package allows such concise definition of graphics Hadley. This really demonstrates the power of your paradigm for ggplot yet again.

Neat! I just used it to chart my iPhone app sales over time:

Would be interesting to see the % daily change (instead of closing price) in this format.

A bug in merge used in original sourceode shifts data one day. Instead use this:

# Merge moves data by one day, avoid
caldat <- data.frame(date.seq = seq(min.date, max.date, by="days"), value = NA)
dates <- as.Date(dates)
caldat$value[match(dates, caldat$date.seq)] <- values

Very nice. Even better - hook this into a web application, make the heat map a clickable image map taking you to data for that date.

Thanks for showcasing this great tool. I outlined it in a blog on machine learning trading methods, and made a small modification to evaluate %daily changes, instead of price values.

Tks for posting and share this tool. I'm going to use in meteorology. Thank you.

Nice, Would you consider adding this in the R Graph Gallery ?

Very appealing graphic. An optional argument to set the
color range, such as ylim=c(nn,nn), would allow
several calendar heat maps to be produced with
the same color scale. This modification
appears to be challenging.
One sloppy way to establish a y range
would be to input a dummy data value
at an obviously false date.

When testing this I get the following error (RStudio 0.94.110 running R 2.14.0):

calendarHeat(stock.data$Date, stock.data$Adj.Close, varname="MSFT Adjusted Close")
Error in compute.layout(x$layout, cond.max.levels, skip = x$skip) :
Inadmissible value of layout.

Any ideas how to fix this? Seems it is the first call to print which gives this error.


If you're using this with the "2006-01-12" start date that's used in the example, then you will exceed the max levels allowed by the print call.

Try again with a more recent start date and you should be fine...

Is there a quick fix how to alter the heat map calendar to Mon first, Sun last layout? Thanks

Can some one help in getting tooltips built into this heat map using library(sendplot) so that upon clicking the cell corresponding to a date, I can see the High/Low of the stock on that date?
An example code to do the above would be greatly welcomed.

Vijayan-you should be able to modify the code in the tutorial vignette to do what you need.

carsX = as.matrix(mtcars)
carsX <- sweep(carsX, 2, colMeans(carsX, na.rm = T))
sx <- apply(carsX, 2, sd, na.rm = T)
carsX <- sweep(carsX, 2, sx, "/")
x = 1:dim(carsX)[2] ; y = 1:dim(carsX)[1] ; z = t(carsX)
plot.call = "image(x=x,y=y, z=z,
axes = FALSE, xlab = '', ylab = '');
las = 2, line = -0.5, tick = 0,cex.axis =1);
las = 2, line = -0.5, tick = 0,cex.axis =.8)"
mai.mat = matrix(c(1,.2,.2,1.5), ncol=4)
mai.prc = FALSE
description=c("Miles/(US) gallon","Number of cylinders",
"Displacement (cu.in.)",
"Gross horsepower",
"Rear axle ratio",
"Weight (lb/1000)",
"1/4 mile time",
"Transmission (0 = automatic, 1 = manual)",
"Number of forward gears",
"Number of carburetors")
mai.mat=mai.mat, mai.prc=mai.prc,
xy.labels = xy.labels,

A simple fix to deal with the "Inadmissible value of layout" error is to replace:

layout = c(1, nyr%%7)


layout = c(1, nyr)

in the source. To accommodate the extra levels, you could decrease the spacing between levels slightly:

between = list(x=0, y=c(0.5, 0.5))

Alternatively, using:

layout = c(1, 7, nyr/7)

yields a multi-page plot, but would need to be tweaked to allow trellis.panelArgs() to function correctly.

The comments to this entry are closed.

R for the Enterprise

Got comments or suggestions for the blog editor?
Email David Smith.
Follow revodavid on Twitter Follow David on Twitter: @revodavid

Search Revolutions Blog