R - d3heatmap - 实现中断

时间:2015-09-02 08:27:40

标签: r colors heatmap

我正在尝试使用d3heatmap包绘制热图。 不幸的是,我还没有成功地使用breaks=...heatmap中的heatmap.2选项来实现某些中断。 这会产生有趣的结果,我甚至不确定我做错了什么或者函数是否忽略了breaks

例如,我尝试过:

breaks = c(seq(-10, -2), seq(-2, -1.65), seq(-1.65, 1.65), seq(1.65, 2), seq(2, 10)

breaks = c(-10, -2, -1.65, 1.65, 2, 10)

colors = c("red", "yellow", "green", "yellow", "red")

但似乎没有任何工作正常。

有什么建议吗?

以下是我的数据dput

 > dput(mat)
 structure(c(-0.04, NA, 0.59, NA, 0.675, 0.96, 1.09, 0.445, NA, 
 0.545, NA, NA, 0.09, -1.11, NA, 0.99, 0.13, 0.215, 1.425, 0, 
 NA, 0.69, 0.805, NA, 0.69, 1.22, NA, 0.3, NA, 0.025, NA, 0.075, 
 0.36, -0.94, NA, -0.31, 0.26, 1.02, -1.19, NA, NA, -0.77, NA, 
 -1.48, 1.05, 0.48, NA, NA, NA, 1.49, -1.285, NA, 0.76, 1.14, 
 -0.62, NA, NA, NA, 0.95, NA, NA, -0.12, 0.49, NA, 2.31, NA, -0.33, 
 0.85, NA, -1.7, -1.63, NA, -1.12, 0.135, -0.18, NA, -0.245, NA, 
 -0.2, -0.2, 0.23, -0.11, NA, 0.3, -0.81, 0.04, 0.18, -0.7, 0.53, 
 0.44, -0.49, 0.28, 0.26, 0.06, 0.265, 0.21, 0.06, -0.175, 0.365, 
 0.255, 1.25, -0.35, 0.16, 0.125, 0.825, 0.08, 0.02, -0.02, 0.99, 
 0.79, -0.23, 0.06, NA, 0.36, -0.64, -0.195, 1.19, -0.29, 0.915, 
 NA, NA, NA, NA, 0.2, 0.1, NA, 0.04, 0.33, NA, 1.46, 2.36, NA, 
 -0.92, 1.295, NA, NA, 0.8, NA, 1.09, 1.45, 5.42, NA, NA, NA, 
 1.69, 3.43, NA, 0.55), .Dim = c(37L, 4L), .Dimnames = list(c("AT", 
 "BE", "BG", "CEE", "CH", "CN", "CZ", "DE", "DK", "EA", "EE", 
 "EMU", "ES", "EU", "FI", "FR", "GB", "GR", "HR", "HU", "IE", 
 "IT", "JP", "LU", "NL", "PL", "PT", "RO", "RS", "RU", "SE", "SI", 
 "SK", "TR", "UA", "UK", "US"), c("Credit Risk", "Funding and liquidity Risk", 
 "Macro Risk", "Market Risk")))

我正在运行的代码:

d3heatmap(abs(mat),
          dendrogram = "none",
          breaks = c(0,1.65,2,10),
          col = c("green", "yellow", "red"),
          na.rm = TRUE)

使用heatmap.2的相同功能完美无缺。

1 个答案:

答案 0 :(得分:4)

函数d3heatmap根本没有'breaks'参数。如果它作为参数传入,则会被忽略。 (参见?d3heatmap。)

另一方面,gplots包中的heatmap.2函数确实有一个“break”参数。这解释了行为上的差异。

幸运的是,通过将适当的'colors'函数传递给d3heatmap仍然可以获得所需的行为。它的工作原理如下。

首先是示例数据:

mat <- structure(c(-0.04, NA, 0.59, NA, 0.675, 0.96, 1.09, 0.445, NA, 
                   0.545, NA, NA, 0.09, -1.11, NA, 0.99, 0.13, 0.215, 1.425, 0, 
                   NA, 0.69, 0.805, NA, 0.69, 1.22, NA, 0.3, NA, 0.025, NA, 0.075, 
                   0.36, -0.94, NA, -0.31, 0.26, 1.02, -1.19, NA, NA, -0.77, NA, 
                   -1.48, 1.05, 0.48, NA, NA, NA, 1.49, -1.285, NA, 0.76, 1.14, 
                   -0.62, NA, NA, NA, 0.95, NA, NA, -0.12, 0.49, NA, 2.31, NA, -0.33, 
                   0.85, NA, -1.7, -1.63, NA, -1.12, 0.135, -0.18, NA, -0.245, NA, 
                   -0.2, -0.2, 0.23, -0.11, NA, 0.3, -0.81, 0.04, 0.18, -0.7, 0.53, 
                   0.44, -0.49, 0.28, 0.26, 0.06, 0.265, 0.21, 0.06, -0.175, 0.365, 
                   0.255, 1.25, -0.35, 0.16, 0.125, 0.825, 0.08, 0.02, -0.02, 0.99, 
                   0.79, -0.23, 0.06, NA, 0.36, -0.64, -0.195, 1.19, -0.29, 0.915, 
                   NA, NA, NA, NA, 0.2, 0.1, NA, 0.04, 0.33, NA, 1.46, 2.36, NA, 
                   -0.92, 1.295, NA, NA, 0.8, NA, 1.09, 1.45, 5.42, NA, NA, NA, 
                   1.69, 3.43, NA, 0.55), .Dim = c(37L, 4L),
                   .Dimnames = list(c("AT", "BE", "BG", "CEE", "CH", "CN", "CZ", "DE", "DK", "EA", "EE", "EMU", "ES", "EU", "FI", "FR", "GB", "GR", "HR", "HU", "IE", "IT", "JP", "LU", "NL", "PL", "PT", "RO", "RS", "RU", "SE", "SI", "SK", "TR", "UA", "UK", "US"), c("Credit Risk", "Funding and liquidity Risk", "Macro Risk", "Market Risk")))

假设我们需要以下三个颜色箱:蓝色表示值&lt; 0,绿色表示值> = 0但是&lt; 0 2,红色表示值&gt; = 2.然后我们定义相应的有序颜色列表。

palette <- c("blue", "green", "red")

我们还定义了颜色箱的边界值。这些值必须包含域边界。

mi <- min(mat, na.rm = TRUE)
ma <- max(mat, na.rm = TRUE)
breaks <- c(mi, 0, 2, ma)

我们现在可以定义一个颜色插值函数,它将[0,1]中的值映射到一个颜色上,尊重我们的颜色区。 'scale'包在这里有所帮助。

install.package('scales') # if needed
library(scales)
colorFunc <- col_bin(palette, bins = rescale(breaks))

最初在我们数据域中定义的中断需要重新调整为[0,1]。 'scale'包中的'rescale'函数处理了这个。

小细节:bin中的bin的低边界包含,但高边界排除。因此值0将为绿色,0到2之间的任何值都将为绿色,但2将为红色。

我们现在可以绘制热图。

d3heatmap(mat, dendrogram = "none", colors = colorFunc, na.rm = TRUE)

结果如下:

enter image description here