3

我有以下数据框:

library(pheatmap)
library(RColorBrewer)

dat <- structure(c(36.28516213, -9.184523335, 5.716034994, 32.82180488, 
-18.5467127, 22.79930301, -24.79075287, -49.23768715), .Dim = c(2L, 
4L), .Dimnames = list(c("motif_a", "motif_b"), c("cow", "dog", 
"snake", "cat")))

dat
#>               cow       dog     snake       cat
#> motif_a 36.285162  5.716035 -18.54671 -24.79075
#> motif_b -9.184523 32.821805  22.79930 -49.23769

并使用以下热图代码:

p <- pheatmap(
  dat,
  color = colorRampPalette(rev(RColorBrewer::brewer.pal(n = 7, name = "RdYlBu")))(1000),
  clustering_method = "ward.D2",
  border_color = "grey60",
  clustering_distance_columns = "correlation",
  clustering_distance_rows = "correlation",
  fontsize_row = 6,
  cluster_rows = TRUE,
  cluster_cols = TRUE,
  scale = "row"
)

我可以得到这个情节。

在此处输入图像描述

正如图中所述,如何将热图中的每个点对应的值转换为右侧色标。

例如,cow motif_b具有 value -9.184523,但右侧色标给出的值介于 0 到 -0.5 之间。我怎样才能得到这些值?

最终结果是一个数据框,其值已转换。我怎样才能做到这一点?

4

2 回答 2

1
scale_rows = function(x){
    m = apply(x, 1, mean, na.rm = T)
    s = apply(x, 1, sd, na.rm = T)
    return((x - m) / s)
}


zscores <- scale_rows(dat)

https://github.com/raivokolde/pheatmap/blob/44a44f7e640c73867f40261691c16dfa4da34fa8/R/pheatmap.r

于 2021-11-01T19:28:48.217 回答
0

当您指定 时,您要求pheatmap绘制介于 -1 和 1 之间的 Pearson 相关性clustering_distance_xxx = "correlation"。运行?pheatmap以获取更多详细信息。

如果你从代码中删除这些部分,那么你会得到你所期望的

library(pheatmap)
library(RColorBrewer)

dat <- structure(c(36.28516213, -9.184523335, 5.716034994, 32.82180488, 
    -18.5467127, 22.79930301, -24.79075287, -49.23768715), .Dim = c(2L, 
    4L), .Dimnames = list(c("motif_a", "motif_b"), c("cow", "dog", 
    "snake", "cat")))

myCol <- colorRampPalette(rev(RColorBrewer::brewer.pal(n = 7, name = "RdYlBu")))(8)

p <- pheatmap(
  dat,
  color = myCol,
  clustering_method = "ward.D2",
  border_color = "grey60",
  clustering_distance_columns = "correlation",
  clustering_distance_rows = "correlation",
  fontsize_row = 6,
  cluster_rows = TRUE,
  cluster_cols = TRUE,
  scale = "row"
)

p1 <- pheatmap(
  dat,
  color = myCol,
  clustering_method = "ward.D2",
  border_color = "grey60"
)

reprex 包(v0.2.0) 于 2018 年 3 月 27 日创建。

于 2018-03-28T04:51:45.007 回答