As far as I know, the native heatmap() function in R will not draw heat maps with color keys.
To this end, install library gplots and use the function heatmap.2(). You can install gplots
through Synaptic in Ubuntu.
1. Install gplots and RColorBrewer for more color options
$ R > install.packages("gplots") > install.packages("RColorBrewer")
2. Copy the script below and save it as “heatmap.r”
library(RColorBrewer) library(gplots) x=read.table("matrix.dat", header=TRUE) mat=data.matrix(x) pdf("heatmap.pdf", height=10, width=10) heatmap.2(mat, Rowv=TRUE, Colv=TRUE, # dendrogram= c("none"), distfun = dist, hclustfun = hclust, xlab = "X data", ylab = "Y data", key=TRUE, keysize=1, trace="none", density.info=c("none"), margins=c(10, 8), col=brewer.pal(10,"PiYG") # col=redgreen(75), ) dev.off()
The commented line dendrogram=c(“none”) will suppress clustering of data in both axes. Another option for coloring the heatmap is given by col=redgreen(75), commonly used in log data in microarray chips.
3. Generate a data matrix as
a b c d e k 0 0 1 1 5 l 1 2 1 6 7 m 1 1 2 9 1 n 0 0 1 3 2 o 9 8 0 6 5
and save it as “matrix.dat”
4. Run as
$ R –vanilla < heatmap.r
5. See the heat map in heatmap.pdf
Summary
Packages used
- gplots
- RColorBrewer
hello: excellent tutorial. it works great!
a question: one axis of the heatmap shows the labels from the header line. The other side has numbers, 4, 11, … where are these coming from?
Thanks for the comment, John
The problem was that I added a – in the top left corner of the matrix to keep the formatting. But this caused that problem with the numbers.
I updated the post removing the – from the matrix. Please copy the new matrix that has a blank character on the top left.
Great tutorial!
Can you format the fonts? Size and style?
Nice tutorial.
When the number of input line is more than 5000 it gives error (Error: evaluation nested too deeply: infinite recursion / options(expressions=)?).
I appreciate your help.
Thanks,
Pankaj
As I tried to “Install gplots and RColorBrewer for more color options” to R, it told me: Warning in install.packages(“gplots”) :
‘lib = “C:/Program Files/R/R-2.13.0/library”‘ is not writable
— Please select a CRAN mirror for use in this session —
Error in contrib.url(repos, type) :
trying to use CRAN without setting a mirror
I have no clue on programs and R, what am I suppose to do now?
Sorry, this tutorial assumes you know how to install packages in R. Besides, I am not a Windows user, so I can’t really help.
But from your error, I would think that you have to be an administrator to install packages. R is saying it doesn’t have permission to save the package in the library “folder”.
I used this tutorial to plot 2 graphs.Can you please let me know if there is any way to scale the values, so I can have the same color key values for both the graphs which makes it easier for comparison. thanks
does this work for non-symmetric matrix?
What do you mean with non-symmetric matrix? To cluster entities, you need to provide distances among them. Distances have certain requirements, one of them is that the distance from A to B is the same as from B to A, so your matrix needs to be symmetric.
@noborujs : Thanks for your reply. I used your example for my data set, however, the colour shades are not consistent. In your example, cell (o,c) and cell (b,k) has the value of 0, so we should expect the same shade (dark pink). Similarly, cell (o,a) and cell (m,d) has the value of 9, the heatmap shows the same colour (dark green). It is not the case for my data, cells with higher values may have lighter shade than cells with lower values. I would like to see if cells have the same values, they should have the exact same color, higher values give darker shade, and lower gives lower shade.
The only think I could think of is that my data has a larger range ( 0 -100), and has 25 rows and 25 columns. Would it make any difference?
Any help on this is greatly appreciated. Thanks.
thank you for sharing:-) it is working nicely!
hello, i am having problem with installing gplot as it says there is no such package..
can anyone please help me with that problem…
I just tried, it installed fine in R (Ubuntu 10.04). All I did was to start R from the command line and then run:
install.packages(“gplots”);
I’m afraid I can’t be of anymore help, sorry.
I have to say a big THANK YOU, I tried different ways to create heatmaps, but this is definitely the best way to go for my purposes. Well, you made my day, sir.
Thanks for the kind comment. I’m glad the post was helpful!
There is one question that comes to my mind: Do you know a way to set cutoffs for the color palette range? My values are ranging from -2 to 0. However, the heatmap color range is set from -2 to 2. My map would be much prettier if the colors were distributed from -2 to 0.
The only way I could come up with would be writing a script to convert the negative values into absolute values, but maybe there is a better solution?
Thanks,
Sebastian
Good question, I’m afraid I don’t have a better solution.
I think I would do what you did, converting values to fit the scale.
However, you might want to keep the values as they are because people are used to the meaning of colors in heatmaps. For example, in gene expression studies, if all values are negative, that would have a biological meaning that you’d alter by rescaling the colors.
Thank you for your quick response. I also found a better way by now that exactly does what I wanted:
# first I define the parameter paris.break with the range I want to cover
> pairs.breaks mycol col = mycol
Sorry for the double post but somehow part of my comment was missing…
Thank you for your quick response. I also found a better way by now that exactly does what I wanted:
first I define the parameter paris.break with the range I want to cover
> pairs.breaks mycol col = mycol
Thank you for your quick response. I also found a better way by now that exactly does what I wanted:
– first I define the parameter paris.break with the range I want to cover
pairs.breaks <- seq(-2.5, 0, by =0.05)
– and accoirding to that I adjust the color panel
mycol <- colorpanel(n=50,low="green",mid="yellow",high="red")
col = mycol
Reblogged this on The Scientific Ocean and commented:
A great post, this is definitely the way to go if you want to create nice heatmaps in R! Quick & easy!
Hi i am new user for R. i had done pyrosequence to asses the bacterial diversity. i dont know how to create heatmap using RDP out put file plz any one help me regard this.