简体   繁体   English

R-d3heatmap-实现中断

[英]R - d3heatmap - implement breaks

I am trying to plot a heatmap using the d3heatmap package. 我正在尝试使用d3heatmap软件包绘制热图。 Unfortunately, I have not been successful yet in implementing certain breaks using the option breaks=... as in heatmap or heatmap.2 . 不幸的是,我还没有成功但在使用选项执行某些休息breaks=...heatmapheatmap.2 This yields just funny results, I am not even sure whether I am doing something wrong or whether the function just ignores breaks . 这只会产生有趣的结果,我什至不确定我是在做错什么,还是该函数是否只是忽略breaks

For example, I tried: 例如,我尝试过:

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

and

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

with

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

but nothing seems to work properly. 但似乎没有任何正常工作。

Any suggestions? 有什么建议么?

Here's the dput of my data: 这里的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")))

And the code I am running: 和我正在运行的代码:

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

The same function using heatmap.2 works perfectly, though. 但是,使用heatmap.2的相同功能可以完美地工作。

The function d3heatmap simply does not have a 'breaks' argument. 函数d3heatmap根本没有'breaks'参数。 If it gets passed in as an argument it is silently ignored. 如果将其作为参数传递,则将被忽略。 (See ?d3heatmap.) (请参阅?d3heatmap。)

The heatmap.2 function in the gplots package on the other hand does have a "breaks" argument. 另一方面,gplots包中的heatmap.2函数确实具有“ breaks”参数。 That explains the difference in behaviour. 这就解释了行为上的差异。

Luckily, it is still possible to get the desired behaviour by passing an appropriate 'colors' function to d3heatmap. 幸运的是,仍然可以通过将适当的“颜色”函数传递给d3heatmap来获得所需的行为。 It works as follows. 它的工作原理如下。

First the example data: 首先是示例数据:

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

Suppose we want the following three color bins: blue for values < 0, green for values >= 0 but < 2, and red for values >= 2. We then define the corresponding ordered list of colors. 假设我们需要以下三个颜色槽:蓝色表示值<0,绿色表示值== 0但<2,红色表示值> =2。然后,我们定义了相应的颜色排序列表。

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

We also define the boundary values of the color bins. 我们还定义了色块的边界值。 These values must include the domain boundaries. 这些值必须包括域边界。

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

We can now define a color interpolation function which maps a value in [0,1] onto a color, respecting our color bins. 现在,我们可以定义一个颜色插值函数,该函数将[0,1]中的值映射到一种颜色上,同时尊重我们的颜色容器。 The 'scales' package comes to help here. “秤”包在这里提供帮助。

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

The breaks originally defined in the domain of our data needed to be rescaled to [0,1]. 最初在数据域中定义的中断需要重新缩放为[0,1]。 The 'rescale' function in the 'scales' package handled that. “ scales”包中的“ rescale”功能可以解决该问题。

Small detail: the low boundary of a bin is included in the bin, but the high boundary is excluded . 小细节:容器的低边界包含在容器中,但高边界被排除在外 So the value 0 will be green, anything between 0 and 2 will be green too, but 2 will be red. 因此,值0将为绿色,0和2之间的任何值也将为绿色,而2将为红色。

We can now plot the heat map. 现在我们可以绘制热图。

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

The result looks like this: 结果看起来像这样:

在此处输入图片说明

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

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