【发布时间】:2019-03-20 10:51:58
【问题描述】:
我有两个时间轴和每个单元格的测量值的数据。从这里我创建了一个热图。我还知道每个单元格的测量是否重要。
我的问题是在所有重要的单元格周围画一条等高线。如果细胞形成具有相同显着性值的簇,我需要在簇周围而不是在每个单独的细胞周围绘制轮廓。
数据格式如下:
x_time y_time metric signif
1 1 1 0.3422285 FALSE
2 2 1 0.6114085 FALSE
3 3 1 0.5381621 FALSE
4 4 1 0.5175120 FALSE
5 1 2 0.6997991 FALSE
6 2 2 0.3054885 FALSE
7 3 2 0.8353888 TRUE
8 4 2 0.3991566 TRUE
9 1 3 0.7522728 TRUE
10 2 3 0.5311418 TRUE
11 3 3 0.4972816 TRUE
12 4 3 0.4330033 TRUE
13 1 4 0.5157972 TRUE
14 2 4 0.6324151 TRUE
15 3 4 0.4734126 TRUE
16 4 4 0.4315119 TRUE
下面的代码生成此数据,其中测量是随机的 (dt$metrics),而重要性是逻辑的 (dt$signif)。
# data example
dt <- data.frame(x_time=rep(seq(1, 4), 4),
y_time=rep(seq(1, 4), each=4),
metric=(rnorm(16, 0.5, 0.2)),
signif=c(rep(FALSE, 6), rep(TRUE, 10)))
可以使用ggplot2的geom_tile单独生成热图
# Generate heatmap using ggplot2's geom_tile
library(ggplot2)
p <- ggplot(data = dt, aes(x = x_time, y = y_time))
p <- p + geom_tile(aes(fill = metric))
基于this question,我设法根据显着性值在每个单元格周围绘制了不同颜色的轮廓。
# Heatmap with lines around each significant cell
p <- ggplot(data = dt, aes(x = x_time, y = y_time))
p <- p + geom_tile(aes(fill = metric, color = signif), size = 2)
p <- p + scale_color_manual(values = c("black", "white"))
但是,这种方法不会通过在整个组周围绘制轮廓来将相邻的重要单元格组合在一起(在我链接到的问题中也有讨论)。
正如this question 所示,可以在指定区域周围绘制框,但我认为这不能扩展到所有可能的单元格簇。
【问题讨论】:
-
raster::clump相当简单。参见例如How to get contour lines around the grids in R-raster?
标签: r ggplot2 heatmap contour significance