ggheatmap, version 2

It looks like my ggplot2 heatmap function gets most traffic on this blog. That’s a bit unfortunate, because it’s the first function I wrote in earnest using ggplot2 and ggplot2 itself has undergone some updates since then, meaning my code is clunky, outdated and, er, broken.

So, with a bit more knowledge of ggplot2 and grid gained over the last few months, I have updated it today, and it is working. I hope it’s useful!

Get the code from github, and run it with

## simulate data
library(mvtnorm) 
sigma=matrix(0,10,10)
sigma[1:4,1:4] <- 0.6
sigma[6:10,6:10] <- 0.8
diag(sigma) <- 1
X <- rmvnorm(n=100,mean=rep(0,10),sigma=sigma)

## make plot
p <- ggheatmap(X)

## display plot
ggheatmap.show(p)

The result should look something like

https://cwcode.wordpress.com/wp-content/uploads/2013/01/wpid-ggheatmap.jpg

pch symbols

I often have used simple code like this to remind myself which number corresponds to which pch character:

par(mar=c(4,1,1,1))
plot(x, y, type="n", ylab="", xlab="pch", axes=FALSE)
box()
axis(1)
abline(v=1:25,lty=3,col="grey")
points(1:25,rep(1,25),pch=1:25)

p2.jpg

I wondered what it would take to do the same in ggplot2. The amount of typing required to remove (most of) the y axis kinda annoys me, but otherwise, it’s quite nice:

ggplot(data.frame(x=x,y=y), aes(x=x,y=y,pch=as.factor(x))) +
  geom_point() +
  xlab("pch") + ylab("") +
  scale_shape_manual(values=1:25) +
  opts(legend.position="none",
       axis.title.y = theme_blank(),
       axis.text.y =  theme_blank() )

p3.jpg