Home » R » ggplot2 » A ggplot2 heatmap function

A ggplot2 heatmap function

The code below is broken. Improved, and working version, in a more recent post.

That said, if you really do want to see the broken version, read on.

I have close to 20 years experience with base graphics in R/Splus. I can do almost anything I like, but ggplot2 definitely wins on visually pleasing stakes. It’s also a nice way of thinking about graphics, once you understand all the components. But it’s also a whole other world. With base graphics, you can take multiple datasets in any shape, add bits and bobs together to get a plot. You don’t have to mess with your data at all, but you may write many lines of plotting code. With ggplot2, the emphasis is more on getting your data in a sensible shape for plotting, and then doing the plot simply in a one liner. At least that is how it works for qplot, and the ability to facet easily without a loop is just lovely. In order to properly learn ggplot I have decided I will not use base graphics for a month.

The first plot I had to make after this decision was a heatmap. I toyed with using pheatmap which is very pretty, but decided that was breaking my promise on the first day. I couldn’t find any good ggplot2 heatmap function either (one that showed dendrograms and heatmap). So I created one, and on the way learnt more than I thought I wanted to know about grid graphics and viewports.

It was also hard to find out exactly how to remove all axes etc using ggplot2. These links helped:

and to move the legend, I used code from

The result is in github.


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