August 3, 2011

(This article was first published on uu kk, and kindly contributed to R-bloggers)

We have an internal image that floated around work several years ago that details network utilization of TCP over a wide variety of configurations. It is a heatmap created in matlab that is just sweet, sweet eye candy. We actually hung it on the outside of a cube for a short while and people couldn’t help but stop and look at it.

It is entirely dysfunctional, mind you. The designer tried to combine eight parameters – with all variations – into a individual 2D plot (3D if you consider color a dimension). It was definitely an internal tool – there were only two or three of us who could decipher the layout enough to say anything about the data. That was fine by us; we basically made up the entire population of people who cared.

Fast forward a few years. I’m currently working on a technical report that could use the data we used to create that plot and, as luck would have it, I’m also the only one from the original group still at the company. In order to be able to include this data in the report there needs to occur a certain amount of reformatting – first my brain, then that plot. I wasn’t the original designer and, although I have access to the code, I don’t know matlab so I’m pretty much stuck. I decided to rework the data in R.

The thing about that original plot was that it had a certain je ne sais quoi: it made you look. I wanted to keep that so I immediately investigated heatmap functionality available in R.

Really? Ouch. Not much available there. I came up with two resources that were helpful: A Wikipedia entry about a Mandelbrot set animation in R; A stackoverflow answer that mentioned rasterImage in a comment. The first site lead me to the color set used in our original plot and the second gave me the pointer I needed to get the job done. I’ll leave what follows as a reminder for myself and a helpful nudge for those who face a similar problem in the future.

hmap.example <- function () {

code <- c("colfun <- colorRampPalette(c(...))",
"my.colors <- colfun(10000)","xs <- 1:100",
"X <- outer(xs,rev(xs))",
"C1 <- matrix(my.colors[X],100,100)", "X <- outer(xs,xs)",
"C2 <- matrix(my.colors[X],100,100)", "X <- outer(rev(xs),xs)",
"C3 <- matrix(my.colors[X],100,100)",
"rasterImage(C2,-100,1,1,100)", "rasterImage(C3,-100,-100,1,1)",
"abline(v=0,col='black',lwd=5)", "abline(h=0,col='black',lwd=5)")

colfun <- colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan",
"#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"))

my.colors <- colfun(10000)
xs <- 1:100
X <- outer(xs,rev(xs))
C1 <- matrix(my.colors[X],100,100)
X <- outer(xs,xs)
C2 <- matrix(my.colors[X],100,100)
X <- outer(rev(xs),xs)
C3 <- matrix(my.colors[X],100,100)

And the result:

To leave a comment for the author, please follow the link and comment on their blog: uu kk. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)