如何在R的matlab heatmap 颜色中自定义颜色

[转载]如何在R的heatmap中自定义颜色
转存~感谢目前非常详细的heatmap画图帖子:
http://pgfe.umassmed.edu/ou/archives/2477
新的地址:
其中,在heatmap.2或者pheatmap中采用colorRampPalette自定义legend的渐变色,可以定义2种、3种等!
col=colorRampPalette(c("black","red"))
pheatmap(data, color = colorRampPalette(c("navy", "white", "firebrick3"))(50), fontsize=9, fontsize_row=6) #自定义颜色
我们在分析了差异表达数据之后,经常要生成一种直观图--热图(heatmap)。这一节就以基因芯片数据为例,示例生成高品质的热图。
钢蓝渐白配色的热图
首先还是从最简单的heatmap开始。
& library(ggplot2)
& library(ALL) #可以使用biocLite("ALL")安装该数据包
& data("ALL")
& library(limma)
& eset&-ALL[,ALL$mol.biol %in% c("BCR/ABL","ALL1/AF4")]
& f&-factor(as.character(eset$mol.biol))
& design&-model.matrix(~f)
& fit&-eBayes(lmFit(eset,design)) #对基因芯片数据进行分析,得到差异表达的数据
& selected
&- p.adjust(fit$p.value[, 2]) &&/span&0.001
& esetSel &- eset[selected,] #选择其中一部分绘制热图
& dim(esetSel) #从这尺度上看,数目并不多,但也不少。如果基因数过多,可以分两次做图。
& library(hgu95av2.db)
& data&-exprs(esetSel)
& probes&-rownames(data)
& symbol&-mget(probes,hgu95av2SYMBOL,ifnotfound=NA)
& symbol&-do.call(rbind,symbol)
& symbol[is.na(symbol[,1]),1]&-rownames(symbol)[is.na(symbol[,1])]
& rownames(data)&-symbol[probes,1] #给每行以基因名替换探针名命名,在绘制热图时直接显示基因名。
& heatmap(data,cexRow=0.5)
使用heatmap函数默认颜色生成的热图
这个图有三个部分,样品分枝树图和基因分枝树图,以及热图本身。之所以对样品进行聚类分析排序,是因为这次的样品本身并没有分组。如果有分组的话,那么可以关闭对样品的聚类分析。对基因进行聚类分析排序,主要是为了色块好看,其实可以选择不排序,或者使用GO聚类分析排序。上面的这种热图,方便简单,效果非常不错。
接下来我们假设样品是分好组的,那么我们想用不同的颜色来把样品组标记出来,那么我们可以使用ColSideColors参数来实现。同时,我们希望变更热图的渐变填充色,可以使用col参数来实现。
& color.map &- function(mol.biol) { if (mol.biol=="ALL1/AF4") "#FF0000" else "#0000FF" }
& patientcolors &- unlist(lapply(esetSel$mol.bio, color.map))
& heatmap(data, col=topo.colors(100), ColSideColors=patientcolors, cexRow=0.5)
使用heatmap函数top.colors填充生成的热图
在heatmap函数中,样品分组只能有一种,如果样品分组有多次分组怎么办?heatmap.plus就是来解决这个问题的。它们的参数都一致,除了ColSideColors和RowSideColors。heatmap使用是一维数组,而heatmap.plus使用的是字符矩阵来设置这两个参数。
& library(heatmap.plus)
& hc&-hclust(dist(t(data)))
& dd.col&-as.dendrogram(hc)
& groups &- cutree(hc,k=5)
& color.map &- function(mol.biol) { if (mol.biol=="ALL1/AF4") 1 else 2 }
& patientcolors &- unlist(lapply(esetSel$mol.bio, color.map))
& col.patientcol&-rbind(groups,patientcolors)
& mode(col.patientcol)&-"character"
& heatmap.plus(data,ColSideColors=t(col.patientcol),cexRow=0.5)
使用heatmap.plus绘制热图
这样绘图的不足是没有热图色key值。gplots中的heatmap.2为我们解决了这个问题。而且它带来了更多的预设填充色。下面就是几个例子。
& library("gplots")
& heatmap.2(data, col=redgreen(75), scale="row", ColSideColors=patientcolors,
key=TRUE, symkey=FALSE, density.info="none", trace="none", cexRow=0.5)
使用heatmap.2函数,readgreen渐变色填充生成的热图
& heatmap.2(data, col=heat.colors(100), scale="row", ColSideColors=patientcolors,
key=TRUE, symkey=FALSE, density.info="none", trace="none", cexRow=0.5)
& heatmap.2(data, col=terrain.colors(100), scale="row", ColSideColors=patientcolors,
key=TRUE, symkey=FALSE, density.info="none", trace="none", cexRow=0.5)
& heatmap.2(data, col=cm.colors(100), scale="row", ColSideColors=patientcolors,
key=TRUE, symkey=FALSE, density.info="none", trace="none", cexRow=0.5)
& heatmap.2(data, col=redblue(100), scale="row", ColSideColors=patientcolors,
key=TRUE, symkey=FALSE, density.info="none", trace="none", cexRow=0.5)
& heatmap.2(data, col=colorpanel(100,low="white",high="steelblue"), scale="row", ColSideColors=patientcolors,
key=TRUE, keysize=1, symkey=FALSE, density.info="none", trace="none", cexRow=0.5)
使用heatmap.2函数,heat.colors渐变色填充生成的热图
使用heatmap.2函数,terrain.colors渐变色填充生成的热图
使用heatmap.2函数,cm.colors渐变色填充生成的热图
使用heatmap.2函数,redblue渐变色填充生成的热图
使用heatmap.2函数,colorpanel渐变色填充生成的热图
然而,以上的heatmap以及heatmap.2虽然方便简单,效果也很不错,可以使用colorpanel方便的设置渐变填充色,但是它的布局没有办法改变,生成的效果图显得有点呆板,不简洁。为此这里介绍如何使用ggplot2当中的geom_tile来为基因芯片绘制理想的热图。
& library(ggplot2)
& hc&-hclust(dist(data))
& rowInd&-hc$order
& hc&-hclust(dist(t(data)))
& colInd&-hc$order
& data.m&-data[rowInd,colInd] #聚类分析的作用是为了色块集中,显示效果好。如果本身就对样品有分组,基因有排序,就可以跳过这一步。
& data.m&-apply(data.m,1,rescale) #以行为基准对数据进行变换,使每一行都变成[0,1]之间的数字。变换的方法可以是scale,rescale等等,按照自己的需要来变换。
& data.m&-t(data.m) #变换以后转置了。
& coln&-colnames(data.m)
& rown&-rownames(data.m) #保存样品及基因名称。因为geom_tile会对它们按坐标重排,所以需要使用数字把它们的序列固定下来。
& colnames(data.m)&-1:ncol(data.m)
& rownames(data.m)&-1:nrow(data.m)
& data.m&-melt(data.m) #转换数据成适合geom_tile使用的形式
& head(data.m)
1 0.1898007
1 0.6627467
1 0.5417057
1 0.4877054
1 0.5096474
1 0.2626248
& base_size&-12 #设置默认字体大小,依照样品或者基因的多少而微变。
& (p &- ggplot(data.m, aes(X2, X1)) + geom_tile(aes(fill = value), #设定横坐标为以前的列,纵坐标为以前的行,填充色为转换后的数据
colour = "white") + scale_fill_gradient(low = "white", #设定渐变色的低值为白色,变值为钢蓝色。
high = "steelblue"))
& p + theme_grey(base_size = base_size) + labs(x = "", #设置xlabel及ylabel为空
y = "") + scale_x_continuous(expand = c(0, 0),labels=coln,breaks=1:length(coln)) + #设置x坐标扩展部分为0,刻度为之前的样品名
scale_y_continuous(expand = c(0, 0),labels=rown,breaks=1:length(rown)) + opts( #设置y坐标扩展部分为0,刻度为之前的基因名
axis.ticks = theme_blank(), axis.text.x = theme_text(size = base_size *
#设置坐标字体为基准的0.8倍,贴近坐标对节,x坐标旋转90度,色彩为中灰
0.8, angle = 90, hjust = 0, colour = "grey50"), axis.text.y = theme_text(
size = base_size * 0.8, hjust=1, colour="grey50"))
使用ggplot2中geom_tile函数,钢蓝渐白配色的热图
也可以很轻松的实现传统渐变填充色,红黄渐变。
& (p &- ggplot(data.m, aes(X2, X1)) + geom_tile(aes(fill = value),
colour = "white") + scale_fill_gradient(low = "yellow",
high = "red"))
& p + theme_grey(base_size = base_size) + labs(x = "",
y = "") + scale_x_continuous(expand = c(0, 0),labels=coln,breaks=1:length(coln)) +
scale_y_continuous(expand = c(0, 0),labels=rown,breaks=1:length(rown)) + opts(
axis.ticks = theme_blank(), axis.text.x = theme_text(size = base_size *
0.8, angle = 90, hjust = 0, colour = "grey50"), axis.text.y = theme_text(
size = base_size * 0.8, hjust=1, colour="grey50"))
使用ggplot2中geom_tile函数,红黄渐变填充的热图
使用红绿渐变填充。
& (p &- ggplot(data.m, aes(X2, X1)) + geom_tile(aes(fill = value),
colour = "white") + scale_fill_gradient(low = "green",
high = "red"))
& p + theme_grey(base_size = base_size) + labs(x = "",
y = "") + scale_x_continuous(expand = c(0, 0),labels=coln,breaks=1:length(coln)) +
scale_y_continuous(expand = c(0, 0),labels=rown,breaks=1:length(rown)) + opts(
axis.ticks = theme_blank(), axis.text.x = theme_text(size = base_size *
0.8, angle = 90, hjust = 0, colour = "grey50"), axis.text.y = theme_text(
size = base_size * 0.8, hjust=1, colour="grey50"))
使用ggplot2中geom_tile函数,红绿渐变填充的热图
使用绿白渐变填充。
& (p &- ggplot(data.m, aes(X2, X1)) + geom_tile(aes(fill = value),
colour = "white") + scale_fill_gradient(low = "seagreen",
high = "white"))
& p + theme_grey(base_size = base_size) + labs(x = "",
y = "") + scale_x_continuous(expand = c(0, 0),labels=coln,breaks=1:length(coln)) +
scale_y_continuous(expand = c(0, 0),labels=rown,breaks=1:length(rown)) + opts(
axis.ticks = theme_blank(), axis.text.x = theme_text(size = base_size *
0.8, angle = 90, hjust = 0, colour = "grey50"), axis.text.y = theme_text(
size = base_size * 0.8, hjust=1, colour="grey50"))
使用ggplot2中geom_tile函数,绿白渐变填充的热图
使用棕白渐变填充。
& (p &- ggplot(data.m, aes(X2, X1)) + geom_tile(aes(fill = value),
colour = "white") + scale_fill_gradient(low = "white",
high = "sienna4"))
& p + theme_grey(base_size = base_size) + labs(x = "",
y = "") + scale_x_continuous(expand = c(0, 0),labels=coln,breaks=1:length(coln)) +
scale_y_continuous(expand = c(0, 0),labels=rown,breaks=1:length(rown)) + opts(
axis.ticks = theme_blank(), axis.text.x = theme_text(size = base_size *
0.8, angle = 90, hjust = 0, colour = "grey50"), axis.text.y = theme_text(
size = base_size * 0.8, hjust=1, colour="grey50"))
使用ggplot2中geom_tile函数,棕白渐变填充的热图
使用灰阶填充。
& (p &- ggplot(data.m, aes(X2, X1)) + geom_tile(aes(fill = value),
colour = "white") + scale_fill_gradient(low = "black",
high = "gray85"))
& p + theme_grey(base_size = base_size) + labs(x = "",
y = "") + scale_x_continuous(expand = c(0, 0),labels=coln,breaks=1:length(coln)) +
scale_y_continuous(expand = c(0, 0),labels=rown,breaks=1:length(rown)) + opts(
axis.ticks = theme_blank(), axis.text.x = theme_text(size = base_size *
0.8, angle = 90, hjust = 0, colour = "grey50"), axis.text.y = theme_text(
size = base_size * 0.8, hjust=1, colour="grey50"))
使用ggplot2中geom_tile函数,灰色渐变填充的热图
除了ggplot2,还有lattice也是不错的选择。我只使用一种填充色,生成两个图,以作示例。
& hc&-hclust(dist(data))
& dd.row&-as.dendrogram(hc)
& row.ord&-order.dendrogram(dd.row) #介绍另一种获得排序的办法
& hc&-hclust(dist(t(data)))
& dd.col&-as.dendrogram(hc)
& col.ord&-order.dendrogram(dd.col)
& data.m&-data[row.ord,col.ord]
& library(ggplot2)
& data.m&-apply(data.m,1,rescale) #rescale是ggplot2当中的一个函数
& library(lattice)
& levelplot(data.m,
aspect = "fill",xlab="",ylab="",
scales = list(x = list(rot = 90, cex=0.8),y=list(cex=0.5)),
colorkey = list(space = "left"),col.regions = heat.colors)
& library(latticeExtra)
& levelplot(data.m,
aspect = "fill",xlab="",ylab="",
scales = list(x = list(rot = 90, cex=0.5),y=list(cex=0.4)),
colorkey = list(space = "left"),col.regions = heat.colors,
list(right =
list(fun = dendrogramGrob, #dendrogramGrob是latticeExtra中绘制树型图的一个函数
list(x = dd.row, ord = row.ord,
side = "right",
size = 5)),
list(fun = dendrogramGrob,
list(x = dd.col,
side = "top",
type = "triangle")))) #使用三角型构图
使用lattice中的levelplot函数,heat.colors填充绘制热图
使用lattice中的levelplot函数,heat.colors填充,dendrogramGrob绘树型,绘制热图
可是可是,绘制一个漂亮的热图这么难么?参数如此之多,设置如此复杂,色彩还需要自己指定。有没有简单到发指的函数呢?有!那就是pheatmap,全称pretty heatmaps.
& library(pheatmap)
& pheatmap(data,fontsize=9, fontsize_row=6) #最简单地直接出图
& pheatmap(data, scale = "row", clustering_distance_row = "correlation", fontsize=9, fontsize_row=6) #改变排序算法
& pheatmap(data, color = colorRampPalette(c("navy", "white", "firebrick3"))(50), fontsize=9, fontsize_row=6) #自定义颜色
& pheatmap(data, cluster_row=FALSE, fontsize=9, fontsize_row=6) #关闭按行排序
& pheatmap(data, legend = FALSE, fontsize=9, fontsize_row=6) #关闭图例
& pheatmap(data, cellwidth = 6, cellheight = 5, fontsize=9, fontsize_row=6) #设定格子的尺寸
& color.map &- function(mol.biol) { if (mol.biol=="ALL1/AF4") 1 else 2 }
& patientcolors &- unlist(lapply(esetSel$mol.bio, color.map))
& hc&-hclust(dist(t(data)))
& dd.col&-as.dendrogram(hc)
& groups &- cutree(hc,k=7)
& annotation&-data.frame(Var1=factor(patientcolors,labels=c("class1","class2")),Var2=groups)
& pheatmap(data, annotation=annotation, fontsize=9, fontsize_row=6) #为样品分组
& Var1 = c("navy", "skyblue")
& Var2 = c("snow", "steelblue")
& names(Var1) = c("class1", "class2")
& ann_colors = list(Var1 = Var1, Var2 = Var2)
& pheatmap(data, annotation=annotation, annotation_colors = ann_colors, fontsize=9, fontsize_row=6) #为分组的样品设定颜色
pheatmap最简单地直接出图
pheatmap改变排序算法
pheatmap自定义颜色
pheatmap关闭按行排序
pheatmap关闭图例
pheatmap设定格子的尺寸
pheatmap为样品分组
pheatmap为分组的样品设定颜色
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。/article/64d05af73bd8.html
利用Excel 2010及以上版本的“条件格式”--“色阶”功能可以制作热图(heatmap)。可选用Adobe Illustrator软件加工、美化之。
这里以Excel 2010为例介绍具体步骤。
Excel 2010或以上版本
Adobe Illustrator
在Excel中选中要做热图的全部数据 (不要选择行列标题!),然后执行以下操作:“条件格式”--“色阶”--“其他规则”
默认是“双色刻度”,可改成“三色刻度”
然后设置最小值、中间值、最大值的类型和颜色
建议将类型都改为“数字”,好处是可以自定义最小值、中间值、最大值的数值大小。然后用windows自带的截图工具(QQ截屏也可以)截取“预览”后面的色条。
注意:你的数据应该在设置的最小值和最大值范围之内。例图中的数据范围是0.3~9.84,因此这里设置最小值0,最大值10,中间值采用0和10的中位数5
将截取的色条粘贴到Excel中,效果如图。
如果觉得色条太长,可以作如下调整:
选中色条,提高Excel的缩放比例,直到能看到色条短边上的小方块,鼠标移动到小方块上,调节色条宽度
在色条上方的对应单元格内输入主要刻度值。
为了美观,可将Excel单元格的填充色都改为白色,并适当调节行高、列高。
到这里,热图基本上就做好了。
觉得数字碍眼?Excel似乎没有透明字体,那就试试Adobe Illustrator吧
复制Excel中的热图,在Illustrator中新建一个空白文档,粘贴。如果粘贴的内容超过了空白文档的边界,请重新新建一个大一点的文档吧。
然后Ctrl+A全选,右键--取消编组
保持全选状态,右键--释放剪切蒙板,然后就可以选择单个的文字了。
删掉热图中所有的数字,然后保存为pdf等格式的文档,用Acrobat打开pdf可另存为图片格式,大功告成!
阅读(...) 评论()r - How to assign your color scale on raw data in heatmap.2() - Stack Overflow
to customize your list.
Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
J it only takes a minute:
I have data that looks like this:
1420468_at_Asb17 0.000 2.328 0.000 0.000 0.000
1430261_at_Rik 1.236 2.050 0.000 0.000 0.000
1431788_at_Fabp12 0.000 2.150 0.000 0.000 0.587
1433187_at_BRik 0.000 2.240 1.343 0.000 1.383
1434430_s_at_Adora2b 0.000 2.006 1.459 0.000 1.272
1435217_at_Gm 2.350 1.494 0.976 0.000
1436717_x_at_Hbb-y 0.000 2.712 0.000 0.000 0.000
1440859_at_Akap6 0.000 2.053 0.000 0.000 1.840
1442625_at_--- 0.000 2.064 1.173 0.000 1.035
1443715_at_Rbm24 0.969 2.219 0.000 0.000 0.000
1445520_at_--- 0.000 2.497 0.000 0.000 0.000
1446035_at_Gm 3.869 0.000 0.000 0.000
1446597_at_Rik 1.000 2.000 0.000 0.000 0.000
1448925_at_Twist2 0.000 2.089 0.938 0.000 0.000
1449711_at_Atp6v1e1 0.605 2.363 2.350 1.094 0.976
1455931_at_Chrna3 0.000 2.354 0.000 0.000 0.000
17 1457647_x_at_Rik 0.000 2.734 0.000 0.000 1.812
1458975_at_--- 0.000 2.079 0.000 0.000 0.000
1459862_at_--- 0.727 2.606 0.000 0.000 1.151
Note in this data (and the actual one) there is no negative values and the positive values
can be as large as 100 or so.
What I want to do is to plot heat map with my own assigned color scale and scheme:
When the value is 0 set it into white.
When the value is == 1 set it into black.
When the value is > 1 set it into shade of reds.
When the value is & 1 and > 0 set it into shade of greens.
also without using any data scaling or built-in z-score transformation.
How can I achieve that?
My current code is this:
library(gplots)
# Read data
dat &- read.table("/1501148/plain/",sep="\t",header=T);
rownames(dat) &- dat$Name
dat &- dat[,!names(dat) %in% c("Name")]
# Clustering and distance measure functions
hclustfunc &- function(x) hclust(x, method="complete")
distfunc &- function(x) dist(x,method="maximum")
Define colours
hmcols &- rev(redgreen(2750));
pdf("~/Desktop/tmp.pdf",height=10)
heatmap.2(as.matrix(dat),Colv=FALSE,dendrogram="row",scale="row",col=hmcols,trace="none", margin=c(5,10), hclust=hclustfunc,distfun=distfunc,lwid=c(1.5,2.0),keysize=1);
Which produces the following plot, where it uses the default z-score row scaling.
11k56155262
The key here is understanding that heatmap.2 uses the col argument in combination with the breaks argument.
Take a look at the code and figure below to see what I mean.
library(gplots)
set.seed(100)
dat = matrix( rexp(25,1/2), ncol=5 )
breaks = 0:5
col = c("green","blue","red","yellow","brown")
heatmap.2( dat, breaks=breaks, col=col )
As you can see, there must be n-1 colors for n breaks. For your particular question, the problem is to map the correct colors to the breaks. I'm using the scale="none" option as @josilber pointed out.
breaks = seq(0,max(dat),length.out=1000)
gradient1 = colorpanel( sum( breaks[-1]&=1 ), "white", "green", "black" )
gradient2 = colorpanel( sum( breaks[-1]&1 ), "black", "red" )
hm.colors = c(gradient1,gradient2)
heatmap.2(as.matrix(dat),scale="none",breaks=breaks,col=hm.colors,
Colv=FALSE,dendrogram="row",trace="none",
margin=c(5,10), hclust=hclustfunc,distfun=distfunc,lwid=c(1.5,2.0))
Another alternative would be to have two gradients: green->black and black->red. Then, you could manually set the zero values to white by making them NA and setting na.color="white".
breaks = seq(0,max(dat),length.out=1000)
gradient1 = colorpanel( sum( breaks[-1]&=1 ), "green", "black" )
gradient2 = colorpanel( sum( breaks[-1]&1 ), "black", "red" )
hm.colors = c(gradient1,gradient2)
dat[dat==0] = NA
heatmap.2(as.matrix(dat),scale="none",breaks=breaks,col=hm.colors,na.color="white",
Colv=FALSE,dendrogram="row",trace="none",
margin=c(5,10), hclust=hclustfunc,distfun=distfunc,lwid=c(1.5,2.0))
And finally, you could just manually edit the gradient for the zero values.
breaks = seq(0,max(dat),length.out=1000)
gradient1 = colorpanel( sum( breaks[-1]&=1 ), "green", "black" )
gradient2 = colorpanel( sum( breaks[-1]&1 ), "black", "red" )
hm.colors = c(gradient1,gradient2)
hm.colors[1] = col2hex("white")
heatmap.2(as.matrix(dat),scale="none",breaks=breaks,col=hm.colors,na.color="white",
Colv=FALSE,dendrogram="row",trace="none",
margin=c(5,10), hclust=hclustfunc,distfun=distfunc,lwid=c(1.5,2.0))
Log fold changes
On another note, it appears that you might be looking at fold changes or some type of ratio. It is fairly common to plot the log fold changes when making a heat map. I "greyed" out the zero values.
dat[dat==0] = NA
heatmap.2( as.matrix(log2(dat)), col=greenred(100),
scale="none", na.color="grey",symbreaks=TRUE,
Colv=FALSE,dendrogram="row",trace="none",
margin=c(5,10), hclust=hclustfunc,distfun=distfunc,lwid=c(1.5,2.0))
For an explanation of @josilber's nice solution:
This code hmcols &- c(colfunc1(200), colfunc2(200*(max(dat) - 1))) makes
a character vector of length 774 (seen by length(hmcols)). Thus, this means that there should be 775 breaks defined. The heatmap.2 function by default makes n+1 breaks where n is the length of the vector used in the col argument. So the number of breaks and colors is worked out, but how does hmcols &- c(colfunc1(200), colfunc2(200*(max(dat) - 1))) map the colors to the breaks correctly? The trick is in clever way that the hmcols vector was created. The number of colors in the first gradient is 200. Since breaks was not explicitly defined, we know that the breaks will be evenly spaced. Since the first gradient goes from 0 to 1 and there are 200 breaks, the width of each break should be 0.005 (or 1/200). Since the second gradient goes from 1 to 3.869 (max(dat)), there should be 2.869/0.005=573.8 breaks (574 breaks when rounding up). Note that the 200*(max(dat) - 1)) d it outputs 573.8. Thus, there are then 200+574 colors mapped to the correct breaks and everything works!
3,00211334
I think there are two things here. The first is how to get rid of the z-scores. This can be done with the scale="none" parameter to heatmap.2.
The other question surrounds your desired gradient. I relied on colorRampPalette for this part. Below, I construct a gradient that goes white -> green -> black for values 0 through 1 and then goes black -> red for values 1 -> max(dat).
library(gplots)
# Read data
dat &- read.table("/1501148/plain/",sep="\t",header=T);
rownames(dat) &- dat$Name
dat &- dat[,!names(dat) %in% c("Name")]
# Clustering and distance measure functions
hclustfunc &- function(x) hclust(x, method="complete")
distfunc &- function(x) dist(x,method="maximum")
Define colours
colfunc1 &- colorRampPalette(c("white", "green", "black"))
colfunc2 &- colorRampPalette(c("black", "red"))
hmcols &- c(colfunc1(200), colfunc2(200*(max(dat) - 1)))
pdf("~/Desktop/tmp.pdf",height=10)
heatmap.2(as.matrix(dat),Colv=FALSE,dendrogram="row",scale="none",col=hmcols,trace="none", margin=c(5,10), hclust=hclustfunc,distfun=distfunc,lwid=c(1.5,2.0),keysize=1);
31.9k113878
The main theme here is to define breaks and specific colors for each break. This can be achieved by using heatmap.2 function.
library(gplots)
library(RColorBrewer)
#Table formatting
rownames(df)&-df[,1] #setting row names
df&-as.matrix(df[,-1])
# Defining breaks for the color scale!
##defining color scale
myCol &- c("white",colorRampPalette(c("green","darkgreen"))(100),"black",colorRampPalette(c("red","darkred"))
#you can change the colors here.
#It is important to have the total number of colors defined for all the breaks.
#i.e if the number of breaks is 100, then there should be 99 colors defined.
#You can change the gradient of the shades by changing no of splots,
#I have used 100 here
##defining breaks
myBreaks &- c(-1,0,seq(1e-5,1-1e-5,length=100),1,seq(1+1e-3,200,length=100))
#set your break start/end and the length here
# I have set it as per your requirements here. Teh shades
#Plotting heatmap
pdf("temporal_data.pdf",width=8,height=8)
hm &- heatmap.2(df, scale="none", Colv=NA,
col = myCol, ## using your colors
breaks = myBreaks, ## using your breaks
dendrogram = "row",
## row dendograms
, cexRow=1, cexCol=1, key=FALSE,
margins = c(2, 12),trace="none")
legend("topleft", fill = c("white","green","black","red"),
legend = c("0", "0.0001 to 0.999", "1","&1"),cex=1,horiz =TRUE)
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
rev .24565
Stack Overflow works best with JavaScript enabled

我要回帖

更多关于 heatmap 颜色设置 的文章

 

随机推荐