{"id":180909,"date":"2024-05-08T20:45:04","date_gmt":"2024-05-08T12:45:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/180909.html"},"modified":"2024-05-08T20:45:15","modified_gmt":"2024-05-08T12:45:15","slug":"r%e8%af%ad%e8%a8%80-ggplot2%e6%9d%a1%e5%bd%a2%e5%9b%be%e6%a8%aa%e8%bd%b4%e6%80%8e%e4%b9%88%e5%88%86%e6%88%90%e4%b8%a4%e9%83%a8%e5%88%86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/180909.html","title":{"rendered":"R\u8bed\u8a00 ggplot2\u6761\u5f62\u56fe\u6a2a\u8f74\u600e\u4e48\u5206\u6210\u4e24\u90e8\u5206"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27070325\/90b20f1f-a0ad-48c0-8b78-5465391ec88e.webp\" alt=\"R\u8bed\u8a00 ggplot2\u6761\u5f62\u56fe\u6a2a\u8f74\u600e\u4e48\u5206\u6210\u4e24\u90e8\u5206\" \/><\/p>\n<p><h3>\u4e00\u3001\u76f4\u63a5\u56de\u7b54\u95ee\u9898<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528R\u8bed\u8a00\u7684ggplot2\u5305\u7ed8\u5236\u6761\u5f62\u56fe\u65f6\uff0c\u8981\u60f3\u5c06\u6a2a\u8f74\u5206\u6210\u4e24\u90e8\u5206\uff0c\u53ef\u4ee5\u901a\u8fc7<strong>\u521b\u9020\u4e00\u4e2a\u4e8c\u7ea7\u5206\u7c7b\u53d8\u91cf\u6765\u5b9e\u73b0\uff0c\u5c06\u6570\u636e\u5206\u7ec4\u540e\u518d\u4f5c\u56fe<\/strong>\u3002\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u5728\u4e00\u4e2a\u56fe\u8868\u4e2d\u5c55\u793a\u66f4\u591a\u7ef4\u5ea6\u7684\u6570\u636e\uff0c\u589e\u52a0\u4fe1\u606f\u7684\u4e30\u5bcc\u6027\u548c\u53ef\u8bfb\u6027\u3002<strong>\u6700\u5e38\u89c1\u7684\u5b9e\u73b0\u65b9\u5f0f\u662f\u5229\u7528<code>facet_wrap()<\/code>\u6216<code>facet_grid()<\/code>\u51fd\u6570\u6765\u5206\u9762\u663e\u793a\u4e0d\u540c\u7684\u7c7b\u522b<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c\u4f7f\u7528<code>facet_wrap()<\/code>\u662f\u975e\u5e38\u76f4\u89c2\u7684\u65b9\u5f0f\uff0c\u5b83\u80fd\u591f\u6839\u636e\u63d0\u4f9b\u7684\u5206\u7c7b\u53d8\u91cf\u5c06\u56fe\u5f62\u6309\u884c\u6216\u5217\u5206\u5f00\u5448\u73b0\uff0c\u4ece\u800c\u5b9e\u73b0\u6a2a\u8f74\u7684\u4e8c\u5206\u6cd5\u89c6\u89c9\u6548\u679c\u3002\u8fd9\u79cd\u65b9\u6cd5\u5c24\u4e3a\u9002\u5408\u5728\u6761\u5f62\u56fe\u4e2d\u5c55\u793a\u4e24\u4e2a\u5c42\u7ea7\u7684\u5206\u7c7b\u4fe1\u606f\uff0c\u5982\u5c06\u6240\u6709\u6570\u636e\u6839\u636e\u4e00\u7ea7\u5206\u7c7b\u5206\u7ec4\uff0c\u7136\u540e\u5728\u6bcf\u7ec4\u5185\u90e8\u6309\u4e8c\u7ea7\u5206\u7c7b\u7ec6\u5206\u5c55\u793a\u4e0d\u540c\u7684\u6761\u5f62\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>facet_wrap()<\/code>\u5206\u9762\u663e\u793a\u6761\u5f62\u56fe<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5047\u8bbe\u4f60\u5df2\u7ecf\u6709\u4e00\u4e2a\u5305\u542b\u5206\u7c7b\u4fe1\u606f\u7684\u6570\u636e\u6846\uff08data frame\uff09\uff0c\u6211\u4eec\u5c06\u901a\u8fc7\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u6765\u8bf4\u660e\u5982\u4f55\u4f7f\u7528<code>facet_wrap()<\/code>\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u51c6\u5907\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u7684\u6570\u636e\u6846\u540d\u4e3a<code>df<\/code>\uff0c\u5b83\u5305\u542b\u81f3\u5c11\u4e09\u5217\uff1a\u4e00\u5217\u662f\u7528\u4e8e\u6761\u5f62\u56fe\u7684\u6570\u503c\uff08\u6bd4\u5982\u9500\u552e\u989d\uff09\u3001\u4e00\u5217\u662f\u4e00\u7ea7\u5206\u7c7b\uff08\u6bd4\u5982\u5e74\u4efd\uff09\u3001\u53e6\u4e00\u5217\u662f\u4e8c\u7ea7\u5206\u7c7b\uff08\u6bd4\u5982\u4ea7\u54c1\u7c7b\u522b\uff09\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u52a0\u8f7dggplot2\u5305<\/strong><\/p>\n<\/p>\n<p><p>\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u5e76\u52a0\u8f7d\u4e86ggplot2\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-R\">library(ggplot2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7ed8\u5236\u57fa\u7840\u6761\u5f62\u56fe\u5e76\u4f7f\u7528<code>facet_wrap()<\/code>\u8fdb\u884c\u5206\u9762<\/strong><\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u7684\u4ee3\u7801\u5c06\u5c55\u793a\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684\u6761\u5f62\u56fe\uff0c\u5e76\u901a\u8fc7<code>facet_wrap()<\/code>\u6309\u4e00\u7ea7\u5206\u7c7b\uff08\u5e74\u4efd\uff09\u8fdb\u884c\u5206\u9762\u663e\u793a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-R\">ggplot(df, aes(x=\u4ea7\u54c1\u7c7b\u522b, y=\u9500\u552e\u989d, fill=\u4e8c\u7ea7\u5206\u7c7b)) +<\/p>\n<p>  geom_bar(stat=&quot;identity&quot;) +<\/p>\n<p>  facet_wrap(~\u5e74\u4efd)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u81ea\u5b9a\u4e49\u5206\u9762\u5c55\u793a<\/strong><\/p>\n<\/p>\n<p><p><code>facet_wrap()<\/code>\u51fd\u6570\u63a5\u53d7\u591a\u4e2a\u53c2\u6570\uff0c\u5141\u8bb8\u4f60\u81ea\u5b9a\u4e49\u5982\u4f55\u5206\u9762\u3002\u6bd4\u5982\uff0c\u901a\u8fc7\u6307\u5b9a<code>ncol<\/code>\u6216<code>nrow<\/code>\u53c2\u6570\u6765\u63a7\u5236\u5217\u6570\u6216\u884c\u6570\u3002\u5982\u679c\u5e0c\u671b\u6bcf\u4e2a\u5206\u9762\u7684\u6761\u5f62\u56fe\u6309\u7167\u9500\u552e\u989d\u964d\u5e8f\u6392\u5217\uff0c\u8fd8\u9700\u8981\u5728\u7ed8\u5236\u6761\u5f62\u56fe\u524d\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>facet_grid()<\/code>\u6df1\u5165\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u4e0e<code>facet_wrap()<\/code>\u7c7b\u4f3c\uff0c<code>facet_grid()<\/code>\u4e5f\u662fggplot2\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u5206\u9762\u51fd\u6570\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u5206\u9762\u63a7\u5236\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5728\u4e24\u4e2a\u7ef4\u5ea6\u4e0a\u8fdb\u884c\u5206\u9762\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u7840\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p><code>facet_grid()<\/code>\u6839\u636e\u516c\u5f0f\u754c\u5b9a\u5982\u4f55\u5206\u9762\u3002\u5982\u679c\u4f60\u60f3\u5728\u7eb5\u8f74\u4f7f\u7528\u4e00\u7ea7\u5206\u7c7b\uff0c\u6a2a\u8f74\u4f7f\u7528\u53e6\u4e00\u4e2a\u5206\u7c7b\uff0c\u5176\u8bed\u6cd5\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-R\">ggplot(df, aes(x=\u4e8c\u7ea7\u5206\u7c7b, y=\u9500\u552e\u989d, fill=\u4ea7\u54c1\u7c7b\u522b)) +<\/p>\n<p>  geom_bar(stat=&quot;identity&quot;) +<\/p>\n<p>  facet_grid(\u5e74\u4efd ~ .)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6df1\u5165\u81ea\u5b9a\u4e49<\/strong><\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0c<code>facet_grid()<\/code>\u7684<code>space<\/code>\u53c2\u6570\u53ef\u4ee5\u63a7\u5236\u4e0d\u540c\u5206\u9762\u7684\u5927\u5c0f\u662f\u5426\u76f8\u7b49\uff0c<code>scale<\/code>\u53c2\u6570\u53ef\u4ee5\u72ec\u7acb\u63a7\u5236\u6bcf\u4e2a\u5206\u9762\u7684x\u6216y\u8f74\u6807\u5ea6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528<code>facet_wrap()<\/code>\u6216<code>facet_grid()<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u5728ggplot2\u7684\u6761\u5f62\u56fe\u4e2d\u8f7b\u677e\u5730\u5b9e\u73b0\u6a2a\u8f74\u7684\u4e8c\u5206\u5c55\u793a\uff0c\u8fd9\u79cd\u65b9\u5f0f\u589e\u52a0\u4e86\u56fe\u8868\u7684\u4fe1\u606f\u5bc6\u5ea6\u4e0e\u89e3\u8bfb\u5c42\u6b21\u3002<strong>\u5728\u5b9e\u8df5\u4e2d\uff0c\u9009\u62e9<code>facet_wrap()<\/code>\u8fd8\u662f<code>facet_grid()<\/code>\u53d6\u51b3\u4e8e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u4ee5\u53ca\u6570\u636e\u672c\u8eab\u7684\u7279\u6027<\/strong>\u3002\u4e3a\u4e86\u8fbe\u5230\u6700\u597d\u7684\u89c6\u89c9\u6548\u679c\u548c\u4fe1\u606f\u4f20\u9012\u6548\u679c\uff0c\u5efa\u8bae\u9002\u5f53\u8c03\u6574\u6761\u5f62\u56fe\u7684\u989c\u8272\u3001\u95f4\u8ddd\u548c\u6392\u5e8f\uff0c\u4ee5\u53ca\u5206\u9762\u5e03\u5c40\u7684\u53c2\u6570\uff0c\u4f7f\u5f97\u56fe\u8868\u65e2\u7f8e\u89c2\u53c8\u5bcc\u6709\u4fe1\u606f\u91cf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u5728ggplot2\u4e2d\u521b\u5efa\u4e00\u4e2a\u5e26\u6709\u5206\u5272\u6a2a\u8f74\u7684\u6761\u5f62\u56fe\uff1f<\/strong><\/p>\n<p><strong>1. \u5982\u4f55\u5728ggplot2\u4e2d\u8bbe\u7f6e\u6a2a\u8f74\u6807\u7b7e\u7684\u5206\u5272\u70b9\uff1f<\/strong><\/p>\n<p>\u4e3a\u4e86\u5c06\u6a2a\u8f74\u5206\u5272\u6210\u4e24\u4e2a\u90e8\u5206\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>scale_x_continuous<\/code>\u51fd\u6570\u5e76\u6307\u5b9a<code>breaks<\/code>\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u60f3\u5c06\u6a2a\u8f74\u5206\u4e3a1\u548c2\u4e4b\u95f4\u7684\u533a\u57df\u548c3\u548c4\u4e4b\u95f4\u7684\u533a\u57df\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n<pre><code>scale_x_continuous(breaks = c(1, 2, 3, 4), labels = c(&quot;1-2&quot;, &quot;3-4&quot;))\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u5728\u6a2a\u8f74\u4e0a\u521b\u5efa\u4e24\u4e2a\u533a\u57df\uff0c\u5e76\u7528\u6807\u7b7e &quot;1-2&quot; \u548c &quot;3-4&quot; \u6765\u8868\u793a\u5b83\u4eec\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u5728ggplot2\u4e2d\u521b\u5efa\u4e00\u4e2a\u5e26\u6709\u5206\u5272\u6a2a\u8f74\u7684\u6761\u5f62\u56fe\uff1f<\/strong><\/p>\n<p>\u8981\u521b\u5efa\u4e00\u4e2a\u5e26\u6709\u5206\u5272\u6a2a\u8f74\u7684\u6761\u5f62\u56fe\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>geom_bar<\/code>\u51fd\u6570\u521b\u5efa\u6761\u5f62\u56fe\uff0c\u5e76\u4f7f\u7528<code>scale_x_continuous<\/code>\u51fd\u6570\u6765\u8bbe\u7f6e\u6a2a\u8f74\u7684\u5206\u5272\u70b9\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-R\">library(ggplot2)\n\n# \u521b\u5efa\u4e00\u4e2a\u6570\u636e\u6846\ndf &lt;- data.frame(category = c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;),\n                 value = c(10, 15, 8, 12))\n\n# \u521b\u5efa\u6761\u5f62\u56fe\nggplot(data = df, aes(x = category, y = value, fill = category)) +\n  geom_bar(stat = &quot;identity&quot;) +\n  scale_x_continuous(breaks = c(1, 2, 3, 4), labels = c(&quot;1-2&quot;, &quot;3-4&quot;))\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u5e26\u6709\u5206\u5272\u6a2a\u8f74\u7684\u6761\u5f62\u56fe\uff0c\u5176\u4e2d\u6a2a\u8f74\u4e0a\u7684\u6807\u7b7e\u4e3a &quot;1-2&quot; \u548c &quot;3-4&quot;\uff0c\u5bf9\u5e94\u7740\u6570\u636e\u6846\u4e2d\u7684\u56db\u4e2a\u7c7b\u522b\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u5728ggplot2\u4e2d\u8bbe\u7f6e\u6bcf\u4e2a\u5206\u5272\u533a\u57df\u7684\u989c\u8272\uff1f<\/strong><\/p>\n<p>\u8981\u4e3a\u6bcf\u4e2a\u5206\u5272\u533a\u57df\u8bbe\u7f6e\u4e0d\u540c\u7684\u989c\u8272\uff0c\u60a8\u53ef\u4ee5\u5728<code>fill<\/code>\u53c2\u6570\u4e2d\u8bbe\u7f6e\u4e00\u4e2a\u77e2\u91cf\u6765\u6307\u5b9a\u6bcf\u4e2a\u7c7b\u522b\u7684\u989c\u8272\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u60f3\u4e3a\u7b2c\u4e00\u4e2a\u5206\u5272\u533a\u57df\u8bbe\u7f6e\u7ea2\u8272\uff0c\u7b2c\u4e8c\u4e2a\u5206\u5272\u533a\u57df\u8bbe\u7f6e\u84dd\u8272\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n<pre><code class=\"language-R\">ggplot(data = df, aes(x = category, y = value, fill = category)) +\n  geom_bar(stat = &quot;identity&quot;) +\n  scale_x_continuous(breaks = c(1, 2, 3, 4), labels = c(&quot;1-2&quot;, &quot;3-4&quot;)) +\n  scale_fill_manual(values = c(&quot;red&quot;, &quot;blue&quot;))\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u4e3a\u7b2c\u4e00\u4e2a\u5206\u5272\u533a\u57df\u8bbe\u4e3a\u7ea2\u8272\uff0c\u7b2c\u4e8c\u4e2a\u5206\u5272\u533a\u57df\u8bbe\u4e3a\u84dd\u8272\u3002\u60a8\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u8bbe\u7f6e\u4e0d\u540c\u7684\u989c\u8272\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e00\u3001\u76f4\u63a5\u56de\u7b54\u95ee\u9898 \u5728\u4f7f\u7528R\u8bed\u8a00\u7684ggplot2\u5305\u7ed8\u5236\u6761\u5f62\u56fe\u65f6\uff0c\u8981\u60f3\u5c06\u6a2a\u8f74\u5206\u6210\u4e24\u90e8\u5206\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u9020\u4e00\u4e2a\u4e8c\u7ea7\u5206\u7c7b\u53d8 [&hellip;]","protected":false},"author":3,"featured_media":180925,"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\/180909"}],"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=180909"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/180909\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/180925"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=180909"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=180909"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=180909"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}