{"id":180030,"date":"2024-05-08T20:26:44","date_gmt":"2024-05-08T12:26:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/180030.html"},"modified":"2024-05-08T20:26:51","modified_gmt":"2024-05-08T12:26:51","slug":"r%e8%af%ad%e8%a8%80%e5%a6%82%e4%bd%95%e5%88%b6%e4%bd%9c%e7%83%ad%e5%9b%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/180030.html","title":{"rendered":"r\u8bed\u8a00\u5982\u4f55\u5236\u4f5c\u70ed\u56fe"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27065045\/5fc69039-7512-4357-9410-b4091a25992d.webp\" alt=\"r\u8bed\u8a00\u5982\u4f55\u5236\u4f5c\u70ed\u56fe\" \/><\/p>\n<p><p>\u70ed\u56fe\u662f\u4e00\u79cd\u6570\u636e\u53ef\u89c6\u5316\u6280\u672f\uff0c\u53ef\u4ee5\u7528\u6765\u8868\u793a\u5927\u5c0f\u3001\u5bc6\u5ea6\u6216\u76f8\u5bf9\u91cd\u8981\u6027\u3002\u5728R\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<strong>\u57fa\u7840\u56fe\u5f62\u5e93\u3001ggplot2\u5e93\u3001\u4ee5\u53ca\u4e13\u95e8\u7684\u70ed\u56fe\u5305\u5982pheatmap<\/strong>\u7b49\u6765\u5236\u4f5c\u70ed\u56fe\u3002<strong>\u5176\u4e2d\uff0c\u4f7f\u7528ggplot2\u5236\u4f5c\u70ed\u56fe\u662f\u4e00\u79cd\u6d41\u884c\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u65e2\u7075\u6d3b\u53c8\u5f3a\u5927<\/strong>\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u4e8eggplot2\u5e93\u7684\u70ed\u56fe\u5236\u4f5c\u7684\u8be6\u7ec6\u6307\u5357\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6570\u636e\u51c6\u5907<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u5236\u4f5c\u70ed\u56fe\uff0c\u9996\u5148\u9700\u8981\u51c6\u5907\u597d\u6570\u636e\u3002\u8fd9\u901a\u5e38\u5305\u62ec\u4e00\u4e2a\u81f3\u5c11\u5305\u542b\u4e09\u5217\u6570\u636e\u7684\u6570\u636e\u6846\uff08data frame\uff09\uff1a\u4e24\u5217\u7528\u6765\u8868\u793a\u70ed\u56fe\u4e0a\u7684\u884c\u548c\u5217\uff0c\u53e6\u4e00\u5217\u5219\u662f\u8fd9\u4e9b\u884c\u548c\u5217\u4ea4\u70b9\u5904\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-r\"># \u793a\u4f8b\u6570\u636e<\/p>\n<p>rows &lt;- rep(c(&quot;Row1&quot;, &quot;Row2&quot;, &quot;Row3&quot;), each = 3)<\/p>\n<p>cols &lt;- rep(c(&quot;Col1&quot;, &quot;Col2&quot;, &quot;Col3&quot;), times = 3)<\/p>\n<p>values &lt;- runif(9, min = 0, max = 100)<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u6846<\/strong><\/h2>\n<p>data_for_heatmap &lt;- data.frame(Rows = rows, Cols = cols, Values = values)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u57fa\u7840\u56fe\u5f62\u5e93<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528R\u7684\u57fa\u7840\u56fe\u5f62\u5e93\u53ef\u4ee5\u5bb9\u6613\u5730\u5236\u4f5c\u51fa\u7b80\u5355\u7684\u70ed\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-r\"># \u8f6c\u6362\u6570\u636e\u4e3a\u77e9\u9635\u683c\u5f0f<\/p>\n<p>matrix_data &lt;- matrix(data_for_heatmap$Values, nrow = 3, ncol = 3)<\/p>\n<h2><strong>\u4f7f\u7528image\u51fd\u6570\u6765\u5236\u4f5c\u70ed\u56fe<\/strong><\/h2>\n<p>image(1:3, 1:3, matrix_data, m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n = &quot;Heatmap Using Base Graphics&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528ggplot2\u5e93<\/h3>\n<\/p>\n<p><p>ggplot2\u662fR\u8bed\u8a00\u4e2d\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u56fe\u5f62\u5e93\uff0c\u53ef\u4ee5\u5236\u4f5c\u51fa\u975e\u5e38\u7cbe\u7f8e\u7684\u70ed\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-r\"># \u52a0\u8f7dggplot2\u5e93<\/p>\n<p>library(ggplot2)<\/p>\n<h2><strong>\u4f7f\u7528ggplot\u6765\u7ed8\u5236\u70ed\u56fe<\/strong><\/h2>\n<p>ggplot(data_for_heatmap, aes(x = Cols, y = Rows, fill = Values)) +<\/p>\n<p>  geom_tile() + <\/p>\n<p>  scale_fill_gradient(low = &quot;blue&quot;, high = &quot;red&quot;) +<\/p>\n<p>  theme_minimal() +<\/p>\n<p>  labs(title = &quot;Heatmap Using ggplot2&quot;, x = &quot;Columns&quot;, y = &quot;Rows&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u4e13\u95e8\u7684\u70ed\u56fe\u5305<\/h3>\n<\/p>\n<p><p>\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4e13\u95e8\u7684\u70ed\u56fe\u5305\uff0c\u5982pheatmap\u6216ComplexHeatmap\uff0c\u5b83\u4eec\u80fd\u63d0\u4f9b\u66f4\u52a0\u4e13\u4e1a\u7684\u70ed\u56fe\u7ed8\u5236\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-r\"># \u52a0\u8f7dpheatmap\u5e93<\/p>\n<p>library(pheatmap)<\/p>\n<h2><strong>\u4f7f\u7528pheatmap\u6765\u7ed8\u5236\u70ed\u56fe<\/strong><\/h2>\n<p>pheatmap(matrix_data, color = colorRampPalette(c(&quot;blue&quot;, &quot;white&quot;, &quot;red&quot;))(100))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u5b9a\u5236\u9009\u9879\uff0c\u4f8b\u5982\u7c07\u96c6\u5206\u6790\uff08clustering\uff09\u3001\u6807\u51c6\u5316\uff08scaling\uff09\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u70ed\u56fe\u7684\u8c03\u6574\u548c\u6539\u8fdb<\/h3>\n<\/p>\n<p><p>\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\u7ed8\u5236\u70ed\u56fe\uff0c\u91cd\u8981\u7684\u662f\u8981\u8fdb\u884c\u8c03\u6574\u4ee5\u6ee1\u8db3\u7279\u5b9a\u7684\u9700\u6c42\uff0c\u4f8b\u5982\u6539\u53d8\u989c\u8272\u4e3b\u9898\u3001\u8c03\u6574\u56fe\u4f8b\u6216\u6dfb\u52a0\u6ce8\u91ca\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-r\"># \u4f7f\u7528ggplot2\u518d\u6b21\u7ed8\u5236\u70ed\u56fe\uff0c\u8fd9\u6b21\u6dfb\u52a0\u4e86\u989d\u5916\u7684\u6ce8\u91ca\u548c\u4e3b\u9898\u8c03\u6574<\/p>\n<p>ggplot(data_for_heatmap, aes(x = Cols, y = Rows, fill = Values)) +<\/p>\n<p>  geom_tile() + <\/p>\n<p>  geom_text(aes(label = round(Values, 1))) + <\/p>\n<p>  scale_fill_gradient(low = &quot;blue&quot;, high = &quot;red&quot;) + <\/p>\n<p>  theme_minimal() + <\/p>\n<p>  theme(<\/p>\n<p>    axis.text.x = element_text(angle = 45, hjust = 1),<\/p>\n<p>    axis.text.y = element_text(size = 10),<\/p>\n<p>    legend.position = &quot;bottom&quot;<\/p>\n<p>  ) +<\/p>\n<p>  labs(title = &quot;Enhanced Heatmap Using ggplot2&quot;, x = &quot;Columns&quot;, y = &quot;Rows&quot;, fill = &quot;Value&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u793a\u4f8b\uff0c\u53ef\u4ee5\u770b\u5230<strong>R\u8bed\u8a00\u63d0\u4f9b\u4e86\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u5de5\u5177\u6765\u5236\u4f5c\u70ed\u56fe<\/strong>\u3002\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u529f\u80fd\u90fd\u80fd\u6ee1\u8db3\u7528\u6237\u5bf9\u6570\u636e\u53ef\u89c6\u5316\u7684\u4e0d\u540c\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u70ed\u56fe\u5236\u4f5c\u9700\u8981\u54ea\u4e9b\u6570\u636e\u51c6\u5907\uff1f<\/strong><\/p>\n<p>\u5728\u5236\u4f5c\u70ed\u56fe\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u51c6\u5907\u4e00\u4e2a\u6570\u636e\u96c6\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u60a8\u60f3\u8981\u8868\u73b0\u7684\u53d8\u91cf\u6216\u6307\u6807\u4ee5\u53ca\u76f8\u5e94\u7684\u6570\u503c\u6570\u636e\u3002\u8fd9\u4e2a\u6570\u636e\u96c6\u53ef\u4ee5\u662f\u4e00\u4e2a\u8868\u683c\u6216\u8005\u77e9\u9635\u5f62\u5f0f\u7684\u6570\u636e\uff0c\u901a\u5e38\u4f7f\u7528\u77e9\u9635\u5f62\u5f0f\u66f4\u65b9\u4fbf\u8fdb\u884c\u70ed\u56fe\u7ed8\u5236\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u4f7f\u7528R\u8bed\u8a00\u7ed8\u5236\u70ed\u56fe\uff1f<\/strong><\/p>\n<p>\u5728R\u8bed\u8a00\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u5e38\u7528\u7684\u5305\u6765\u7ed8\u5236\u70ed\u56fe\uff0c\u6bd4\u5982ggplot2\u3001pheatmap\u3001heatmap.2\u7b49\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u52a0\u8f7d\u8fd9\u4e9b\u5305\u5230R\u73af\u5883\u4e2d\u3002\u7136\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u7279\u5b9a\u7684\u51fd\u6570\u6765\u8bfb\u53d6\u5e76\u5904\u7406\u60a8\u51c6\u5907\u597d\u7684\u6570\u636e\u96c6\uff0c\u5c06\u5176\u8f6c\u6362\u4e3a\u9002\u5408\u7ed8\u5236\u70ed\u56fe\u7684\u5f62\u5f0f\u3002\u63a5\u4e0b\u6765\uff0c\u60a8\u53ef\u4ee5\u8bbe\u7f6e\u70ed\u56fe\u7684\u6837\u5f0f\u3001\u8c03\u6574\u989c\u8272\u6620\u5c04\u7b49\u53c2\u6570\u3002\u6700\u540e\uff0c\u4f7f\u7528\u7279\u5b9a\u7684\u51fd\u6570\u5c06\u6570\u636e\u96c6\u7ed8\u5236\u6210\u70ed\u56fe\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u6216\u663e\u793a\u51fa\u6765\u3002<\/p>\n<p><strong>3. \u70ed\u56fe\u6709\u54ea\u4e9b\u5e94\u7528\u573a\u666f\uff1f<\/strong><\/p>\n<p>\u70ed\u56fe\u5728\u6570\u636e\u53ef\u89c6\u5316\u4e2d\u6709\u5e7f\u6cdb\u5e94\u7528\uff0c\u53ef\u4ee5\u7528\u4e8e\u5c55\u793a\u6570\u636e\u7684\u5206\u5e03\u3001\u76f8\u5173\u6027\u3001\u8d8b\u52bf\u7b49\u3002\u70ed\u56fe\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\u5305\u62ec\uff1a\u57fa\u56e0\u8868\u8fbe\u8c31\u5206\u6790\u3001\u75be\u75c5\u6570\u636e\u5206\u6790\u3001\u5e02\u573a\u9700\u6c42\u5206\u6790\u3001\u6c14\u5019\u53d8\u5316\u7814\u7a76\u7b49\u3002\u901a\u8fc7\u70ed\u56fe\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u89c2\u5730\u89c2\u5bdf\u5230\u4e0d\u540c\u6570\u636e\u70b9\u4e4b\u95f4\u7684\u5dee\u5f02\u548c\u5173\u8054\u6027\uff0c\u6709\u52a9\u4e8e\u53d1\u73b0\u9690\u85cf\u5728\u6570\u636e\u80cc\u540e\u7684\u89c4\u5f8b\u548c\u8d8b\u52bf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u70ed\u56fe\u662f\u4e00\u79cd\u6570\u636e\u53ef\u89c6\u5316\u6280\u672f\uff0c\u53ef\u4ee5\u7528\u6765\u8868\u793a\u5927\u5c0f\u3001\u5bc6\u5ea6\u6216\u76f8\u5bf9\u91cd\u8981\u6027\u3002\u5728R\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u57fa\u7840\u56fe\u5f62\u5e93\u3001ggplot2\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":180045,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/180030"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=180030"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/180030\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/180045"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=180030"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=180030"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=180030"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}