问题描述
我已经使用heatmap.2
在R中制作了一个热图,我唯一不知道的是如何控制颜色键上刻度线的存在和标记.我在下面有一个例子.我想做的是在颜色键的开始和结束处都带有刻度线,以指示值的范围(在这种情况下为0-1),而不是6个带有0、0.4和0.8标签的刻度线.我敢肯定有一种简单的方法可以控制它,但我似乎找不到它.
I've made a heatmap in R using heatmap.2
and the only thing I can't figure out is how to control the presence and labeling of tick marks on the color key. I have an example below. What I would like to do is have tick marks at the beginning and end of the color key to indicate the range of values (in this case 0-1), rather than 6 tick marks with labels at 0, 0.4 and 0.8. I'm sure there is a simple way to control this but I can't seem to find it.
library('gplots')
data <- c(0, 0.1, 0.1, 0.2, 0.2, 0.7, 0.7, 0.8, 1)
matr <- matrix(data, 3, 3)
heatmap.2(matr, trace="none", density.info="none")
我能找到的唯一解决方法是直接更改heatmap.2
本身以接受其他参数,因为这似乎是硬编码的(在我的情况下,我想为颜色键添加一个最小和最大范围).
The only fix I can find is to directly change heatmap.2
itself to accept additional arguments as this seems to be hardcoded (in my case I want to add a min and max range for the color key).
原始热图.2
heatmap.2 <-function (...)
{
...
lv <- pretty(breaks) # line 362
...
}
更改为:
heatmap.2 <-function (..., xMin = NULL, xMax = NULL, ...)
{
...
if(is.null(xMin)) lv <- pretty(breaks)
else lv <- c(xMin, xMax)
...
}
推荐答案
"key.xtickfun"参数就是您想要的.将函数传递给此参数,该函数返回一个命名列表,该列表的元素将传递给轴"函数.
The "key.xtickfun" argument is what you want. Pass a function to this argument that returns a named list whose elements will be passed to the "axis" function.
在函数中,我从父框架(gplots环境)获得中断",使其漂亮,并仅保留第一个和最后一个中断,即最小和最大.然后,返回带有"at"和"labels"的列表. "at"是从break01调用的scale01函数(也是从父框架)返回的值. 标签"是标签,应该是中断本身.
In the function, I get "breaks" from the parent frame (the gplots environment), make them pretty, and keep only the first and last break, which are the min and max. Then, return a list with "at" and "labels". "at" is the values returned from the scale01 function (also from the parent frame) called on the breaks. "labels" are the labels, which should be the breaks themselves.
heatmap.2(matr, trace="none", density.info="none",
key.xtickfun = function() {
breaks = pretty(parent.frame()$breaks)
breaks = breaks[c(1,length(breaks))]
list(at = parent.frame()$scale01(breaks),
labels = breaks)
})
这篇关于heatmap.2和颜色键刻度线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!