简体   繁体   English

R中带有heatmap.2的热图

[英]heat maps in R with heatmap.2

I am trying to create a heatmap in R. I have tried using the heatmap.2 command. 我试图在R中创建一个热图。我尝试使用heatmap.2命令。

My data consists of x, and y columns and associated relative frequency of a point(frequency divided by the total frequency) 我的数据包括x和y列以及一个点的相关相对频率(频率除以总频率)

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. 我已经将数据转换为矩阵,因为这似乎是heatmap命令想要的,并试图删除x和y坐标。

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. 我看到有两个问题,没有列出所有的x和y坐标。 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. 即x坐标从1开始到35,而y坐标从11开始到30。热图函数似乎并不喜欢这样。 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. 我考虑过在数据集的开头插入零,以便y轴上从1到10的每个点都有一个值。 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. 我应该补充一点,如果可能的话,我也希望在x和y刻度上显示数字。 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: 这是我正在使用的heatmap命令:

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] 考虑到您的3列数据存储在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 尝试使用ggplot2,它会产生更好的热图

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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