{"id":375,"date":"2017-03-15T00:34:07","date_gmt":"2017-03-14T16:34:07","guid":{"rendered":"https:\/\/www.zhuyanbin.com\/?p=375"},"modified":"2017-03-15T00:34:07","modified_gmt":"2017-03-14T16:34:07","slug":"r-heatmap-2%e4%bd%9c%e5%9b%be%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.yanbin888.com\/?p=375","title":{"rendered":"R heatmap.2\u4f5c\u56fe\u8be6\u89e3"},"content":{"rendered":"<p>library gplots\u4e2d\u6709\u4e00\u4e2a\u5f88\u5b9e\u7528\u7684\u70ed\u56fe\u5de5\u5177\uff0c\u5c31\u662fheatmap.2<br \/>\n\u63d0\u4ea4\u7684\u6570\u636e\u4e3a\u77e9\u9635\uff0c\u884c\u5217name\u53ef\u4f5c\u4e3a\u805a\u7c7b\u540e\u7684\u6807\u7b7e<br \/>\n\u5176\u4e2d\u6709\u5f88\u5b9e\u7528\u7684\u53c2\u6570<br \/>\n1.\u00a0<wbr \/> \u884c\u3001\u5217\u805a\u7c7b\u6811\u72b6\u56fe,\u70ed\u56fe\uff0c\u4ee5\u53cacolorkey\u7684\u4f4d\u7f6e\u6392\u5e03<br \/>\n<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/> \u53ef\u4ee5\u7528\u53c2\u6570lmat,\u5176\u4e2d1\u4ee3\u8868\u70ed\u56fe\uff0c2\u4ee3\u8868\u884c\u805a\u7c7b\u6811\u72b6\u56fe\uff0c3\u4ee3\u8868\u5217\u805a\u7c7b\u6811\u72b6\u56fe\uff0c4\u4ee3\u8868colorkey,0\u4ee3\u8868\u7a7a<br \/>\n<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u9ed8\u8ba4\u60c5\u51b5\u4e0blmat=rbind(c(4,3),c(2,1))\u00a0<wbr \/> \uff0c\u5373colorkey\u5728\u5de6\u4e0a\u89d2<br \/>\n<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/> \u6839\u636elmat\uff0c\u753b\u5e03\u88ab\u5206\u4e3a\u4e24\u884c\u3001\u4e24\u5217\uff0c\u76f8\u5e94\u7684\u4f60\u53ef\u4ee5\u8bbe\u7f6e\u6bcf\u884c\u6bcf\u5217\u7684\u5bbd\u548c\u9ad8\uff0c\u8fd9\u5c31\u662f\u53c2\u6570lwid\u4ee5\u53calhei\u3002\u6240\u4ee5lwid\u5411\u91cf\u7684\u957f\u5ea6\u8981\u4e0elmat\u4e2d\u5217\u7684\u6570\u76ee\u76f8\u7b49\uff0clhei\u5411\u91cf\u7684\u957f\u5ea6\u8981\u4e0elmat\u4e2d\u884c\u7684\u6570\u76ee\u76f8\u7b49<br \/>\n<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/> \u4f8b\u5982\u00a0<wbr \/>\u4f60 \u60f3\u628acolorkey\u653e\u5728\u56fe\u7684\u4e0b\u65b9\uff0c\u53ef\u8bbe\u7f6e lmat=rbind(c(0,3),c(2,1),c(0,4))\u00a0<wbr \/>,lhei=c(1,4,2),lwid=c(1,2)\u00a0<wbr \/>\u3002\u5176\u4e2d\u7684\u957f\u548c\u5bbd\u5176\u5b9e\u5c31\u662f\u4ee3 \u8868\u753b\u5e03\u4e0a\u4f60\u5206\u914d\u7ed9\u5404\u90e8\u5206\u7684\u91cf\uff0c\u4f8b\u5982\u6b64\u5904lhei=c(1,4,2),\u5219\u5927\u7ea6\u6574\u4e2a\u753b\u5e03\u9ad8\u5ea6\u76841\/7\u7ed9\u7b2c\u4e00\u884c\uff0c4\/7\u7ed9\u7b2c\u4e8c\u884c\uff0c2\/7\u00a0<wbr \/>\u7ed9\u7b2c\u4e09\u884c\u00a0<wbr \/>\u00a0<wbr \/><br \/>\n2.\u00a0<wbr \/>\u00a0<wbr \/>\u884c\u3001\u5217\u6807\u7b7e\u5b57\u4f53\u5927\u5c0f<br \/>\n<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/> \u53ef\u4ee5\u4f7f\u7528\u53c2\u6570cexCol\u3001cexRow\u8bbe\u7f6e<br \/>\n3\u00a0<wbr \/>\u00a0<wbr \/> \u989c\u8272\u8bbe\u7f6e\u4f7f\u7528col<br \/>\n<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u53ef\u4ee5\u81ea\u5df1\u5b9a\u4e49\u989c\u8272\u5411\u91cf\uff0c\u989c\u8272\u4ee3\u7801\u53ef\u53c2\u8003<a href=\"http:\/\/canghai2381.blog.163.com\/blog\/static\/3400332420082192317234\/%C2%A0\" target=\"_blank\">http:\/\/canghai2381.blog.163.com\/blog\/static\/3400332420082192317234\/\u00a0<wbr \/><\/a>;\u00a0<wbr \/><br \/>\n<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/> \u76f8\u5e94\u7684\u53ef\u4ee5\u7528breaks\u53c2\u6570\u754c\u5b9a\u989c\u8272\u5206\u5272\u7684\u9608\u503c<br \/>\n4\u00a0<wbr \/> colorkey\u7684\u663e\u793a<br \/>\n<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/> key=FALSE\u53ef\u4e0d\u663e\u793acolorkey,\u800cdensity.info\u53ef\u4ee5\u786e\u5b9a\u8981\u4e0d\u8981\u5728colorkey\u4e0a\u663e\u793a\u76f4\u65b9\u56fe\u00a0<wbr \/><br \/>\n5\u00a0<wbr \/> \u884c\u3001\u5217\u6807\u7b7e\u7684\u5206\u7c7b\u989c\u8272<br \/>\n<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/> RowSideColors\u3001 ColSideColors\u53c2\u6570\u53ef\u4ee5\u4e3a\u6bcf\u4e00\u884c\/\u5217\u5b9a\u4e49\u5bf9\u5e94\u5206\u7c7b\u7684\u989c\u8272\uff0c\u4f7f\u7528\u8be5\u989c\u8272\u6807\u8bb0\u6bcf\u4e00\u884c\u6216\u8005\u5217\u6570\u636e\u6240\u5c5e\u7684\u7c7b\u578b<br \/>\n<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/> \u4f8b\u5982\u6709\u516d\u5217\u6570\u636e\uff0c\u5206\u4e3a\u4e24\u79cd\u7c7b\u578b\uff0c\u5206\u522b\u7528\u7ea2\u7eff\u6807\u8bb0 ColSideColors=c(&#8216;green&#8217;,&#8217;green&#8217;,&#8217;green&#8217;,&#8217;red&#8217;,&#8217;red&#8217;,&#8217;red&#8217;)<br \/>\n6 \u805a\u7c7b\u56fe\u989c\u8272\u8303\u56f4<br \/>\n<wbr \/>\u00a0<wbr \/> \u505a\u805a\u7c7b\u56fe\u989c\u8272\u5f88\u91cd\u8981\uff0c\u4e00\u822c\u91c7\u7528greenred\u6216\u8005redgreen\uff0c\u4f46\u662f\u6709\u65f6\u53ef\u80fd\u9700\u8981\u5176\u4ed6\u6e10\u53d8\u7684\u989c\u8272\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528<span style=\"color: #0c0c0c;\">colorRampPalette\u53c2\u6570\uff0c\u4f8b\u5982\u60f3\u8981\u7eff\u767d\u84dd\u6e10\u53d8\uff0c\u5c31\u53ef\u4ee5\u8bbe\u7f6e<\/span>col=colorRampPalette(c(&#8220;green&#8221;,&#8221;white&#8221;,&#8221;red&#8221;))<br \/>\n7 \u989c\u8272\u5206\u5272\u70b9<br \/>\n<wbr \/>\u00a0<wbr \/> \u4f60\u8bbe\u7f6e\u7684\u989c\u8272\u53ef\u4ee5\u9009\u62e9\u662f\u4e0d\u662f\u4ee50\u4e3a\u5bf9\u79f0\uff0c\u5982\u679c\u60f3\u5bf9\u79f0\uff0c\u5c31\u53ef\u4ee5\u4ee4symkey=TRUE<br \/>\n<wbr \/>\u00a0<wbr \/> \u53e6\u5916\uff0c\u5c31\u662f\u4f60\u53ef\u80fd\u9700\u8981\u8bbe\u7f6e\u67d0\u4e00\u6570\u503c\u8303\u56f4\u4e3a\u4e00\u4e2a\u989c\u8272\uff0c\u8fd9\u5c31\u53ef\u4ee5\u4f7f\u7528breaks\u53c2\u6570\uff0c\u4f8b\u5982\u4f60\u7684\u989c\u8272\u6709\u516b\u79cd<br \/>\n<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/> \u5b9a\u4e49\u51fd\u6570colorsChoice&lt;- colorRampPalette(c(&#8220;green&#8221;,&#8221;white&#8221;,&#8221;red&#8221;))<br \/>\n<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/> \u4ea7\u751f8\u4e2a\u6e10\u53d8col=colorsChoice(8)<br \/>\n<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/> 8\u79cd\u989c\u8272\u5bf9\u5e94\u7684\u6570\u503c\u533a\u95f4breaks=c(-3,-2,-1,0,1,2,3,4,5)<\/p>\n<p>#\u753b\u5e26\u884c\u5217\u5206\u7c7bcolor\u7684heatmap<br \/>\nlibrary(gplots)<br \/>\nlibrary(RColorBrewer)<br \/>\nmydata&lt;-center.exp<br \/>\nhclustfunc &lt;- function(x) hclust(x, method=&#8221;complete&#8221;)<br \/>\ndistfunc &lt;- function(x) dist(x, method=&#8221;euclidean&#8221;)<\/p>\n<p># perform clustering on rows and columns<br \/>\ncl.row &lt;- hclustfunc(distfunc(mydata))<br \/>\ncl.col &lt;- hclustfunc(distfunc(t(mydata)))<\/p>\n<p># extract cluster assignments; i.e. k=8 (rows) k=5 (columns)<br \/>\ngr.row &lt;- cutree(cl.row, k=8)<br \/>\ngr.col &lt;- cutree(cl.col, k=5)<br \/>\n# require(RColorBrewer)<br \/>\ncol1 &lt;- brewer.pal(8, &#8220;Set1&#8221;)<br \/>\ncol2 &lt;- brewer.pal(5, &#8220;Pastel1&#8221;)<\/p>\n<p># require(gplots)\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/><br \/>\nheatmap.2(as.matrix(mydata), hclustfun=hclustfunc, distfun=distfunc,\u00a0<wbr \/>\u00a0<wbr \/><br \/>\n<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/> RowSideColors=col1[gr.row], ColSideColors=col2[gr.col])<br \/>\n<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/><\/p>\n<p>#extract cluster based on dist<br \/>\ngr.row &lt;- cutree(cl.row,h=2 )<br \/>\ngr.col &lt;- cutree(cl.col, h=2)<\/p>\n<p># require(RColorBrewer)<br \/>\ncol1 &lt;- colorRampPalette(brewer.pal(0, &#8220;Set1&#8221;))(length(unique(gr.row)))<br \/>\ncol2 &lt;- colorRampPalette(brewer.pal(5, &#8220;Pastel1&#8221;))(length(unique(gr.col)))<\/p>\n<p># require(gplots)\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/><br \/>\nheatmap.2(as.matrix(mydata), hclustfun=hclustfunc, distfun=distfunc,\u00a0<wbr \/>\u00a0<wbr \/><br \/>\n<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/> RowSideColors= col1[gr.row] , trace=&#8221;none&#8221;,col=greenred,Colv=FALSE)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>library gplots\u4e2d\u6709\u4e00\u4e2a\u5f88\u5b9e\u7528\u7684\u70ed\u56fe\u5de5\u5177\uff0c\u5c31\u662fheatmap.2 \u63d0\u4ea4\u7684\u6570\u636e\u4e3a\u77e9\u9635\uff0c\u884c\u5217name\u53ef <span class=\"ellipsis\">&hellip;<\/span> <span class=\"more-link-wrap\"><a href=\"https:\/\/www.yanbin888.com\/?p=375\" class=\"more-link\"><span>Read More &rarr;<\/span><\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[41],"class_list":["post-375","post","type-post","status-publish","format-standard","hentry","category-r","tag-r"],"_links":{"self":[{"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=\/wp\/v2\/posts\/375","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=375"}],"version-history":[{"count":1,"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=\/wp\/v2\/posts\/375\/revisions"}],"predecessor-version":[{"id":376,"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=\/wp\/v2\/posts\/375\/revisions\/376"}],"wp:attachment":[{"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}