Home » R » ggplot2 » ggheatmap, version 2

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
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

The result should look something like



3 thoughts on “ggheatmap, version 2

  1. Hi,

    I really appreciate your code. I couldn’t find any aesthetically pleasing heatmaps in R packages, so the combination with ggplot is really nice. One small issue: the column labels weren’t reordering to reflect their clustering. I made one small change and it seemed to fix it. On line 119, I changed “labels = colnames(x)” to “labels = col.hc$labels[col.hc$order]”.

    Thanks again!

  2. Hi,

    Thanks for this nice code.
    I want to ask how to show the row names label also?
    I tried to change the code ‘row.plot <- mydplot(row.dendro, row=TRUE, labels=TRUE) +
    scale_y_continuous(breaks = 1:nrow(x),labels=row.hc$labels[row.hc$order])
    theme(plot.margin = unit(rep(0, 4), "lines"))', while it's not working. Thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s