{"id":180566,"date":"2024-05-08T20:37:59","date_gmt":"2024-05-08T12:37:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/180566.html"},"modified":"2024-05-08T20:38:05","modified_gmt":"2024-05-08T12:38:05","slug":"r%e8%af%ad%e8%a8%80ggplot%e7%bb%98%e5%88%b6%e6%9f%b1%e7%8a%b6%e5%9b%be%e5%a6%82%e4%bd%95%e8%b0%83%e6%95%b4%e5%a1%ab%e5%85%85%e9%a2%9c%e8%89%b2%e9%a1%ba%e5%ba%8f%e5%92%8c%e5%9b%be%e4%be%8b%e4%b8%80","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/180566.html","title":{"rendered":"R\u8bed\u8a00ggplot\u7ed8\u5236\u67f1\u72b6\u56fe\u5982\u4f55\u8c03\u6574\u586b\u5145\u989c\u8272\u987a\u5e8f\u548c\u56fe\u4f8b\u4e00\u81f4"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27065837\/434bda7e-bfbb-4b6e-89e9-bd9445e9205e.webp\" alt=\"R\u8bed\u8a00ggplot\u7ed8\u5236\u67f1\u72b6\u56fe\u5982\u4f55\u8c03\u6574\u586b\u5145\u989c\u8272\u987a\u5e8f\u548c\u56fe\u4f8b\u4e00\u81f4\" \/><\/p>\n<p><p>\u5728\u4f7f\u7528R\u8bed\u8a00\u7684ggplot2\u5305\u7ed8\u5236\u67f1\u72b6\u56fe\u65f6\uff0c<strong>\u8c03\u6574\u586b\u5145\u989c\u8272\u7684\u987a\u5e8f\u4ee5\u53ca\u4f7f\u5176\u4e0e\u56fe\u4f8b\u4e00\u81f4<\/strong> \u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\u3002\u8fd9\u4e3b\u8981\u6d89\u53ca\u5230\u4e24\u4e2a\u4e3b\u8981\u6b65\u9aa4\uff1a<strong>\u9996\u5148\uff0c\u901a\u8fc7\u5bf9\u6570\u636e\u8fdb\u884c\u56e0\u5b50\u7ea7\u522b\u7684\u8c03\u6574\u6765\u6539\u53d8\u67f1\u72b6\u56fe\u7684\u586b\u5145\u989c\u8272\u987a\u5e8f\uff1b\u5176\u6b21\uff0c\u5229\u7528scale_fill_manual()\u51fd\u6570\u6765\u624b\u52a8\u8bbe\u7f6e\u989c\u8272\u548c\u56fe\u4f8b\u7684\u4e00\u81f4\u6027\u3002<\/strong> \u5728\u8be6\u7ec6\u63cf\u8ff0\u4e4b\u524d\uff0c\u9700\u91cd\u70b9\u5f3a\u8c03\u7684\u662f\uff0c\u786e\u4fdd\u67f1\u72b6\u56fe\u7684\u989c\u8272\u987a\u5e8f\u4e0e\u56fe\u4f8b\u5bf9\u5e94\uff0c\u4e0d\u4ec5\u8ba9\u56fe\u8868\u770b\u8d77\u6765\u66f4\u52a0\u6574\u6d01\uff0c\u4e5f\u4f7f\u5f97\u4fe1\u606f\u4f20\u9012\u66f4\u4e3a\u51c6\u786e\u660e\u6670\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6539\u53d8\u67f1\u72b6\u56fe\u7684\u586b\u5145\u989c\u8272\u987a\u5e8f\u901a\u5e38\u662f\u901a\u8fc7\u5bf9\u6570\u636e\u96c6\u4e2d\u76f8\u5e94\u53d8\u91cf\u7684\u56e0\u5b50\u7ea7\u522b\u8fdb\u884c\u8c03\u6574\u5b9e\u73b0\u7684\u3002\u5728R\u8bed\u8a00\u4e2d\uff0cfactor()\u51fd\u6570\u5141\u8bb8\u7528\u6237\u6307\u5b9a\u56e0\u5b50\u7684\u7ea7\u522b\uff08levels\uff09\uff0c\u8fdb\u800c\u5f71\u54cd\u56e0\u5b50\u5728ggplot2\u751f\u6210\u7684\u56fe\u8868\u4e2d\u7684\u663e\u793a\u987a\u5e8f\u3002\u5f53\u8fd9\u4e9b\u7ea7\u522b\u88ab\u6307\u5b9a\u540e\uff0cggplot2\u5728\u7ed8\u5236\u56fe\u8868\u65f6\u4f1a\u6309\u7167\u8fd9\u4e00\u6307\u5b9a\u987a\u5e8f\u6765\u5b89\u6392\u5404\u7c7b\u522b\u7684\u663e\u793a\u548c\u5176\u5bf9\u5e94\u7684\u989c\u8272\u586b\u5145\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u51c6\u5907\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u8981\u5728ggplot2\u4e2d\u7ed8\u5236\u67f1\u72b6\u56fe\uff0c\u9996\u5148\u9700\u8981\u51c6\u5907\u9002\u5f53\u7684\u6570\u636e\u3002\u5047\u5b9a\u6211\u4eec\u6709\u4e00\u4e2a\u6570\u636e\u6846\uff0c\u5b83\u5305\u542b\u4e86\u4e00\u7cfb\u5217\u7c7b\u522b\u53ca\u5176\u5bf9\u5e94\u7684\u6570\u503c\u3002\u505a\u4e3a\u6f14\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5305\u62ec\u5546\u54c1\u79cd\u7c7b\u548c\u9500\u91cf\u7684\u7b80\u5355\u6570\u636e\u6846\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-R\">library(ggplot2)<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data &lt;- data.frame(<\/p>\n<p>  Category = factor(c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;), levels = c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;)),<\/p>\n<p>  Sales = c(23, 45, 10, 30)<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u6539\u53d8\u586b\u5145\u989c\u8272\u7684\u987a\u5e8f<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u901a\u8fc7\u8c03\u6574\u56e0\u5b50\u7ea7\u522b\u7684\u65b9\u6cd5\uff0c\u6765\u6539\u53d8\u67f1\u72b6\u56fe\u7684\u586b\u5145\u989c\u8272\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-R\"># \u5c06Category\u5217\u7684\u7ea7\u522b\u8fdb\u884c\u91cd\u65b0\u6392\u5e8f\uff0c\u4ee5\u6539\u53d8\u67f1\u72b6\u56fe\u4e2d\u7684\u989c\u8272\u987a\u5e8f<\/p>\n<p>data$Category &lt;- factor(data$Category, levels = c(&quot;D&quot;, &quot;C&quot;, &quot;B&quot;, &quot;A&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7ed8\u5236\u67f1\u72b6\u56fe\u5e76\u8c03\u6574\u989c\u8272<\/h3>\n<\/p>\n<p><p>\u6709\u4e86\u6570\u636e\u4e4b\u540e\uff0c\u5c31\u53ef\u4ee5\u5229\u7528ggplot2\u6765\u7ed8\u5236\u67f1\u72b6\u56fe\u5e76\u901a\u8fc7<code>scale_fill_manual()<\/code>\u6765\u624b\u52a8\u8bbe\u7f6e\u989c\u8272\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-R\">ggplot(data, aes(x = Category, y = Sales, fill = Category)) +<\/p>\n<p>  geom_bar(stat = &quot;identity&quot;) +<\/p>\n<p>  scale_fill_manual(values = c(&quot;D&quot; = &quot;blue&quot;, &quot;C&quot; = &quot;red&quot;, &quot;B&quot; = &quot;green&quot;, &quot;A&quot; = &quot;yellow&quot;)) + <\/p>\n<p>  theme_minimal()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u786e\u4fdd\u989c\u8272\u987a\u5e8f\u548c\u56fe\u4f8b\u4e00\u81f4<\/h3>\n<\/p>\n<p><p>\u6700\u540e\u4e00\u6b65\u662f\u786e\u4fdd\u7ed8\u5236\u7684\u67f1\u72b6\u56fe\u4e2d\u586b\u5145\u989c\u8272\u7684\u987a\u5e8f\u4e0e\u56fe\u4f8b\u7684\u987a\u5e8f\u4e00\u81f4\u3002\u5728\u524d\u4e00\u6b65\u9aa4\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u901a\u8fc7<code>scale_fill_manual()<\/code>\u624b\u52a8\u6307\u5b9a\u4e86\u989c\u8272\uff0c\u8fd9\u540c\u65f6\u4e5f\u5b9a\u4e49\u4e86\u56fe\u4f8b\u4e2d\u5404\u7c7b\u522b\u7684\u989c\u8272\u3002\u56e0\u4e3a\u5728\u6307\u5b9a\u56e0\u5b50\u7ea7\u522b\u65f6\u6211\u4eec\u5df2\u7ecf\u91cd\u65b0\u5b9a\u4e49\u4e86\u7c7b\u522b\u7684\u987a\u5e8f\uff0c\u6240\u4ee5\u8fd9\u65f6\u7684\u586b\u5145\u989c\u8272\u987a\u5e8f\u548c\u56fe\u4f8b\u4e2d\u7684\u987a\u5e8f\u662f\u4e00\u81f4\u7684\u3002<\/p>\n<\/p>\n<p><p><strong>\u603b\u7ed3\u800c\u8a00\uff0c\u901a\u8fc7\u8c03\u6574\u6570\u636e\u56e0\u5b50\u7684\u7ea7\u522b\u548c\u4f7f\u7528<code>scale_fill_manual()<\/code>\u51fd\u6570\u6765\u624b\u52a8\u8bbe\u7f6e\u989c\u8272\uff0c\u80fd\u591f\u6709\u6548\u5730\u8c03\u6574ggplot2\u7ed8\u5236\u7684\u67f1\u72b6\u56fe\u4e2d\u7684\u586b\u5145\u989c\u8272\u987a\u5e8f\uff0c\u540c\u65f6\u786e\u4fdd\u8fd9\u4e00\u987a\u5e8f\u4e0e\u56fe\u4f8b\u4fdd\u6301\u4e00\u81f4\u6027\u3002<\/strong> \u8fd9\u4e0d\u4ec5\u589e\u52a0\u4e86\u56fe\u8868\u7684\u7f8e\u89c2\u5ea6\uff0c\u800c\u4e14\u4e5f\u4f7f\u5f97\u4fe1\u606f\u7684\u4f20\u8fbe\u66f4\u4e3a\u6e05\u6670\u51c6\u786e\u3002\u501f\u52a9\u8fd9\u79cd\u65b9\u6cd5\uff0c\u7528\u6237\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u56fe\u8868\u7684\u5c55\u793a\uff0c\u66f4\u597d\u5730\u6ee1\u8db3\u5404\u79cd\u6570\u636e\u53ef\u89c6\u5316\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u8c03\u6574R\u8bed\u8a00ggplot\u7ed8\u5236\u7684\u67f1\u72b6\u56fe\u7684\u586b\u5145\u989c\u8272\u987a\u5e8f\uff1f<\/strong><\/p>\n<p>\u8981\u8c03\u6574R\u8bed\u8a00ggplot\u7ed8\u5236\u7684\u67f1\u72b6\u56fe\u7684\u586b\u5145\u989c\u8272\u987a\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>scale_fill_manual<\/code>\u51fd\u6570\u3002\u9996\u5148\uff0c\u5728<code>aes<\/code>\u51fd\u6570\u4e2d\u8bbe\u7f6e<code>fill<\/code>\u53c2\u6570\u4e3a\u7528\u4e8e\u5206\u7ec4\u7684\u53d8\u91cf\uff0c\u7136\u540e\u4f7f\u7528<code>scale_fill_manual<\/code>\u51fd\u6570\u6765\u8bbe\u7f6e\u586b\u5145\u989c\u8272\u7684\u987a\u5e8f\u3002\u5c06\u8be5\u51fd\u6570\u7684<code>values<\/code>\u53c2\u6570\u8bbe\u7f6e\u4e3a\u4e00\u4e2a\u5305\u542b\u4f60\u60f3\u8981\u7684\u989c\u8272\u987a\u5e8f\u7684\u5411\u91cf\u5373\u53ef\u3002<\/p>\n<p><strong>2. \u600e\u6837\u624d\u80fd\u4f7fR\u8bed\u8a00ggplot\u7ed8\u5236\u7684\u67f1\u72b6\u56fe\u7684\u56fe\u4f8b\u548c\u586b\u5145\u989c\u8272\u4e00\u81f4\uff1f<\/strong><\/p>\n<p>\u4e3a\u4e86\u4f7fR\u8bed\u8a00ggplot\u7ed8\u5236\u7684\u67f1\u72b6\u56fe\u7684\u56fe\u4f8b\u548c\u586b\u5145\u989c\u8272\u4e00\u81f4\uff0c\u4f60\u9700\u8981\u5728<code>aes<\/code>\u51fd\u6570\u4e2d\u8bbe\u7f6e<code>fill<\/code>\u53c2\u6570\u4e3a\u8be5\u56fe\u4f8b\u6240\u5bf9\u5e94\u7684\u53d8\u91cf\u3002\u5982\u679c\u4f60\u60f3\u8981\u6539\u53d8\u56fe\u4f8b\u7684\u6807\u7b7e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>labs<\/code>\u51fd\u6570\u6765\u81ea\u5b9a\u4e49\u6807\u7b7e\u540d\u79f0\u3002<\/p>\n<p><strong>3. \u5728R\u8bed\u8a00ggplot\u7ed8\u5236\u7684\u67f1\u72b6\u56fe\u4e2d\uff0c\u5982\u679c\u60f3\u8981\u81ea\u5b9a\u4e49\u6bcf\u4e2a\u67f1\u5b50\u7684\u586b\u5145\u989c\u8272\uff0c\u5e94\u8be5\u5982\u4f55\u64cd\u4f5c\uff1f<\/strong><\/p>\n<p>\u5982\u679c\u60f3\u8981\u81ea\u5b9a\u4e49R\u8bed\u8a00ggplot\u7ed8\u5236\u7684\u67f1\u72b6\u56fe\u4e2d\u6bcf\u4e2a\u67f1\u5b50\u7684\u586b\u5145\u989c\u8272\uff0c\u4f60\u53ef\u4ee5\u5728<code>geom_bar<\/code>\u51fd\u6570\u4e2d\u8bbe\u7f6e<code>fill<\/code>\u53c2\u6570\u4e3a\u4f60\u60f3\u8981\u7684\u989c\u8272\u4ee3\u7801\u3002\u53ef\u4ee5\u4f7f\u7528\u6807\u51c6\u989c\u8272\u540d\u79f0\u3001\u5341\u516d\u8fdb\u5236\u989c\u8272\u4ee3\u7801\u6216RGB\u503c\u6765\u8868\u793a\u989c\u8272\u3002\u8fd9\u6837\uff0c\u6bcf\u4e2a\u67f1\u5b50\u7684\u586b\u5145\u989c\u8272\u5c31\u4f1a\u6839\u636e\u4f60\u7684\u8bbe\u7f6e\u800c\u6539\u53d8\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728\u4f7f\u7528R\u8bed\u8a00\u7684ggplot2\u5305\u7ed8\u5236\u67f1\u72b6\u56fe\u65f6\uff0c\u8c03\u6574\u586b\u5145\u989c\u8272\u7684\u987a\u5e8f\u4ee5\u53ca\u4f7f\u5176\u4e0e\u56fe\u4f8b\u4e00\u81f4 \u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\u3002\u8fd9\u4e3b\u8981\u6d89\u53ca [&hellip;]","protected":false},"author":3,"featured_media":180576,"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\/180566"}],"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=180566"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/180566\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/180576"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=180566"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=180566"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=180566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}