简体   繁体   中英

heat maps in R with heatmap.2

I am trying to create a heatmap in R. I have tried using the heatmap.2 command.

My data consists of x, and y columns and associated relative frequency of a point(frequency divided by the total frequency)

with zero frequency in most places and a small relative frequency in others. I have converted my data to a matrix since this seems what the heatmap command wants and attempted to remove the x and y-coordinates.

BB <- matrix(as.matrix(surfacerevfreq1[, 3]),  ncol = 35, byrow = T)

There are two problems as I see it, not all x and y -coordinates are listed. ie the x-coordinate starts at 1 and goes to 35 whereas the y-coordinate starts at 11 and goes to 30. The heatmap function does not seem to like this. I have thought over inserting zeros at the beggining of the data set so that each point from 1- 10 on the y-axis had a value. However, I am not sure if this is the best idea.

So I suppose my question is: how do I format my data in such a way that I can a valid heatmap from this data? I should add that I want the numbers on my x and y-scale displayed also if possible. Since the purpose it to graphically show the relative frequency associated with each point.

thanks.

here is a subset of my data so that you can see what it looks like in matrix form

    col1         col2         col3         col4         col5         col6            col7         col8         col9        col10        col11        col12 
0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000    0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 
   col13        col14        col15        col16        col17         col18            col19        col20        col21        col22        col23        col24 
0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000  0.0000000000 0.0000000000 0.0000000000 0.0005017561 0.0005017561 0.0000000000 
   col25        col26        col27        col28        col29            col30            col31        col32        col33        col34        col35 
0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000      0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 

and here is the heatmap command that I am using:

hU <- heatmap.2(xx, Rowv=FALSE, symm=TRUE,  trace="none", density.info="none",   dendrogram="none")

Try this, considering your 3 columns data is stored in data[x, y, q]

a = matrix(0, nrow = 30 - 10, ncol = 35)
rownames(a) = 11:30
apply(data, 1, function(x) a[x[2] - 10, x[1]] <<- x[3])
heatmap.2(a, Rowv=FALSE, symm=TRUE,  trace="none", density.info="none", 
          dendrogram="none", xlab="X", ylab="Y")

Try using ggplot2, it produces a lot nicer heatmaps

Example:

data <- read.csv("data.txt", header=T, sep=",")
attach(data)
data.m = melt(data) 
data.m <- ddply(data.m, .(variable), transform, rescale = rescale(value))
require(ggplot2)
p <- ggplot(data.m, aes(variableX, variableY)) + geom_tile(aes(fill = rescale), 
  colour =   "white") + scale_fill_gradient(low = "red", high = "green")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM