r - heatmap.2 指定行顺序还是防止重新排序?

标签 r matrix heatmap

我正在尝试使用 heatmap.2(下面的代码)生成一些对数转换的倍数变化数据图。

我想按照最后一列中的值(从大到小)对热图中的行进行排序。这些行正在自动排序(我不确定“幕后”使用的精确计算),如图所示,正在执行一些聚类。

样本数据

gid     2hrs    4hrs    6hrs    8hrs
1234    0.5     0.75    0.9     2
2234    0       0       1.5     2
3234    -0.5    0.1     1       3
4234    -0.2    -0.2    0.4     2
5234    -0.5    1.2     1      -0.5
6234    -0.5    1.3     2      -0.3
7234    1       1.2    0.5      2
8234    -1.3    -0.2    2       1.2
9234    0.2     0.2     0.2     1
0123    0.2     0.2     3       0.5

代码

data <- read.csv(infile, sep='\t',comment.char="#")
rnames <- data[,1]                 # assign labels in column 1 to "rnames"
mat_data <- data.matrix(data[,2:ncol(data)])  # transform columns into a matrix
rownames(mat_data) <- rnames                  # assign row names

# custom palette
my_palette <- colorRampPalette(c("turquoise", "yellow", "red"))(n = 299)
# (optional) defines the color breaks manually for a "skewed" color transition
col_breaks = c(seq(-4,-1,length=100),  # for red
seq(-1,1,length=100),              # for yellow
seq(1,4,length=100))              # for green

# plot data
heatmap.2(mat_data,
density.info="none",  # turns off density plot inside color legend
trace="none",         # turns off trace lines inside the heat map
margins =c(12,9),     # widens margins around plot
col=my_palette,       # use on color palette defined earlier
breaks=col_breaks,    # enable color transition at specified limits
dendrogram='none',     # only draw a row dendrogram
Colv=FALSE)            # turn off column clustering

情节

enter image description here

我想知道是否有人可以建议如何关闭重新排序,以便我可以按最后一列重新排序矩阵并强制使用此顺序,或者破解 heatmap.2 函数来执行此操作。

最佳答案

您未指定 Rowv=FALSE,默认情况下,行会重新排序(在 heatmap.2 帮助中,对于参数 Rowv :

determines if and how the row dendrogram should be reordered. By default, it is TRUE, which implies dendrogram is computed and reordered based on row means. If NULL or FALSE, then no dendrogram is computed and no reordering is done.

因此,如果您想根据最后一列对行进行排序,您可以这样做:

mat_data<-mat_data[order(mat_data[,ncol(mat_data)],decreasing=T),]

然后

heatmap.2(mat_data,
density.info="none",  
trace="none",         
margins =c(12,9),    
col=my_palette,       
breaks=col_breaks,   
dendrogram='none',     
Rowv=FALSE,
Colv=FALSE)     

您将得到以下图像: enter image description here

关于r - heatmap.2 指定行顺序还是防止重新排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27959044/

相关文章:

r - 如何抑制 Sweave 中未被 echo=FALSE 抑制的输出?

r - 从 R gamlss 对象预测新拟合值时出错

c++ - 浏览字符串矩阵并将其存储在 vector 的 vector 中

r - R plotly中的离散颜色条

python - 如何排序我的数据以在 Bokeh 中制作热图?

r - 将固定和可变参数传递给 Optimx

r - 在 R 中的 ggplot 中绘制树

r - 颜色按行缩放时的热图

algorithm - 以特定方式对 3x3 矩阵进行排序的最少步骤数

r - 错误 : all entries of 'x' must be nonnegative and finite in fisher. 测试