{"id":294121,"date":"2024-05-20T15:02:27","date_gmt":"2024-05-20T07:02:27","guid":{"rendered":""},"modified":"2024-05-20T15:02:42","modified_gmt":"2024-05-20T07:02:42","slug":"r%e8%af%ad%e8%a8%80ggplot%e5%a6%82%e4%bd%95%e6%b7%bb%e5%8a%a0%e5%9b%be%e4%be%8b-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/294121.html","title":{"rendered":"R\u8bed\u8a00ggplot\u5982\u4f55\u6dfb\u52a0\u56fe\u4f8b"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27141139\/e7a95e6a-c4d5-4236-aaf5-29318f45f29c.webp\" alt=\"R\u8bed\u8a00ggplot\u5982\u4f55\u6dfb\u52a0\u56fe\u4f8b\" \/><\/p>\n<p><p><strong>R\u8bed\u8a00\u7684ggplot2\u5305\u4e2d\u6dfb\u52a0\u56fe\u4f8b\u901a\u5e38\u662f\u81ea\u52a8\u751f\u6210\u7684\uff0c\u5f53\u4f60\u6620\u5c04\u4e86\u4e00\u4e2a aesthetic\uff08\u5982\u989c\u8272\u3001\u5f62\u72b6\u3001\u5927\u5c0f\u7b49\uff09\u5230\u4e00\u4e2a\u53d8\u91cf\u65f6\uff0cggplot\u4f1a\u521b\u5efa\u4e00\u4e2a\u5bf9\u5e94\u7684\u56fe\u4f8b<\/strong>\u3002\u4f8b\u5982\u3001\u5728\u4f7f\u7528<code>geom_point()<\/code>\u521b\u5efa\u6563\u70b9\u56fe\u65f6\uff0c\u5982\u679c\u4f60\u6307\u5b9a<code>aes(color=factor(\u67d0\u4e2a\u53d8\u91cf))<\/code>\uff0c\u90a3\u4e48\u6563\u70b9\u56fe\u7684\u989c\u8272\u5c31\u4f1a\u6839\u636e\u8be5\u53d8\u91cf\u7684\u4e0d\u540c\u6c34\u5e73\u6765\u53d8\u5316\uff0c\u5e76\u4e14ggplot2\u4f1a\u81ea\u52a8\u6dfb\u52a0\u5305\u542b\u8fd9\u4e9b\u989c\u8272\u6620\u5c04\u8bf4\u660e\u7684\u56fe\u4f8b\u3002<strong>\u5982\u679c\u9700\u8981\u624b\u52a8\u8c03\u6574\u56fe\u4f8b\u7684\u5185\u5bb9\u3001\u4f4d\u7f6e\u6216\u6837\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e3b\u9898\u548c\u6307\u5357\u51fd\u6570\u6765\u5b9e\u73b0<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u81ea\u52a8\u56fe\u4f8b\u751f\u6210<\/h3>\n<\/p>\n<p><p>\u5f53\u5728ggplot\u4e2d\u6620\u5c04\u4e0d\u540c\u7684aesthetic\uff0c\u6bd4\u5982color\u3001fill\u3001shape\u7b49\u5230\u6570\u636e\u96c6\u7684\u67d0\u4e2a\u53d8\u91cf\u65f6\uff0c\u56fe\u4f8b\u5c06\u81ea\u52a8\u51fa\u73b0\u5728\u56fe\u8868\u7684\u5408\u9002\u4f4d\u7f6e\u3002\u4ee5\u4e0b\u4e3a\u521b\u5efa\u5e26\u6709\u81ea\u52a8\u56fe\u4f8b\u7684\u57fa\u7840\u6563\u70b9\u56fe\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-R\">library(ggplot2)<\/p>\n<h2><strong>\u5047\u5b9a\u6709\u4e00\u7ec4\u6570\u636eframe<\/strong><\/h2>\n<p>df &lt;- data.frame(<\/p>\n<p>  X = rnorm(100),<\/p>\n<p>  Y = rnorm(100),<\/p>\n<p>  Group = factor(rep(1:2, each=50))<\/p>\n<p>)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6563\u70b9\u56fe\uff0c\u5e76\u5c06Group\u6620\u5c04\u5230\u989c\u8272\u4e0a<\/strong><\/h2>\n<p>ggplot(df, aes(x=X, y=Y, color=Group)) +<\/p>\n<p>  geom_point()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4fee\u6539\u56fe\u4f8b\u6807\u9898\u548c\u6807\u7b7e<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u56fe\u4f8b\u7684\u81ea\u52a8\u751f\u6210\u6807\u9898\u6216\u8005\u6807\u7b7e\u4e0d\u7b26\u5408\u9700\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528<code>labs()<\/code>\u51fd\u6570\u6765\u81ea\u5b9a\u4e49\u56fe\u4f8b\u7684\u6807\u9898\u548c\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-R\">ggplot(df, aes(x=X, y=Y, color=Group)) +<\/p>\n<p>  geom_point() +<\/p>\n<p>  labs(color = &quot;Group Type&quot;, x = &quot;X Axis&quot;, y = &quot;Y Axis&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u8c03\u6574\u56fe\u4f8b\u6837\u5f0f<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4e00\u7cfb\u5217\u7684theme\u5143\u7d20\u6765\u5b9a\u5236\u56fe\u4f8b\u7684\u6837\u5f0f\uff0c\u4f8b\u5982\u4f4d\u7f6e\u3001\u6587\u672c\u5b57\u4f53\u3001\u80cc\u666f\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-R\">ggplot(df, aes(x=X, y=Y, color=Group)) +<\/p>\n<p>  geom_point() +<\/p>\n<p>  theme(legend.position = &quot;top&quot;, # \u56fe\u4f8b\u4f4d\u7f6e<\/p>\n<p>        legend.text = element_text(size = 10), # \u56fe\u4f8b\u6587\u672c\u5927\u5c0f<\/p>\n<p>        legend.background = element_rect(fill = &quot;lightblue&quot;), # \u56fe\u4f8b\u80cc\u666f<\/p>\n<p>        legend.key = element_blank()) # \u56fe\u4f8b\u952e\u7684\u80cc\u666f\u8bbe\u4e3a\u900f\u660e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u56fe\u4f8b\u4f4d\u7f6e\u8c03\u6574<\/h3>\n<\/p>\n<p><p>\u56fe\u4f8b\u7684\u4f4d\u7f6e\u53ef\u4ee5\u8bbe\u7f6e\u4e3a&quot;left&quot;\u3001&quot;right&quot;\u3001&quot;bottom&quot;\u3001&quot;top&quot;\u6216\u8005\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u5750\u6807\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-R\">ggplot(df, aes(x=X, y=Y, color=Group)) +<\/p>\n<p>  geom_point() +<\/p>\n<p>  theme(legend.position = &quot;bottom&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u79fb\u9664\u56fe\u4f8b<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u79fb\u9664\u56fe\u4f8b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>guides()<\/code>\u6216<code>theme()<\/code>\u51fd\u6570\u5c06\u56fe\u4f8b\u8bbe\u7f6e\u4e3a\u201cnone\u201d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-R\">ggplot(df, aes(x=X, y=Y, color=Group)) +<\/p>\n<p>  geom_point() +<\/p>\n<p>  theme(legend.position=&quot;none&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005<\/p>\n<\/p>\n<p><pre><code class=\"language-R\">ggplot(df, aes(x=X, y=Y, color=Group)) +<\/p>\n<p>  geom_point() +<\/p>\n<p>  guides(color=FALSE)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4ea4\u4e92\u5f0f\u56fe\u4f8b<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u590d\u6742\u7684\u53ef\u89c6\u5316\u6848\u4f8b\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u56fe\u4f8b\u6709\u4ea4\u4e92\u529f\u80fd\uff0c\u5373\u70b9\u51fb\u56fe\u4f8b\u5f71\u54cd\u56fe\u5f62\u7684\u663e\u793a\u5185\u5bb9\u3002\u4f7f\u7528<code>ggplotly()<\/code>\u51fd\u6570\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-R\">library(plotly)<\/p>\n<h2><strong>\u521b\u5efa\u7b80\u5355\u7684ggplot\u56fe\u5f62<\/strong><\/h2>\n<p>p &lt;- ggplot(df, aes(x=X, y=Y, color=Group)) + geom_point()<\/p>\n<h2><strong>\u8f6c\u6362\u4e3aplotly\u5bf9\u8c61<\/strong><\/h2>\n<p>ggplotly(p)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u4ee5\u4e0a\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5bf9ggplot2\u751f\u6210\u7684\u56fe\u8868\u4e2d\u7684\u56fe\u4f8b\u8fdb\u884c\u6dfb\u52a0\u3001\u4fee\u6539\u548c\u5b9a\u5236\uff0c\u4ece\u800c\u589e\u5f3a\u56fe\u8868\u7684\u53ef\u8bfb\u6027\u548c\u4e13\u4e1a\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u4f7f\u7528ggplot\u6dfb\u52a0\u56fe\u4f8b\uff1f<\/strong><\/p>\n<p>\u6dfb\u52a0\u56fe\u4f8b\u662fggplot\u7ed8\u56fe\u4e2d\u7684\u4e00\u9879\u5e38\u89c1\u4efb\u52a1\u3002\u60a8\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u6765\u6dfb\u52a0\u56fe\u4f8b\uff1a<\/p>\n<ol>\n<li><strong>\u6307\u5b9a\u8981\u6dfb\u52a0\u56fe\u4f8b\u7684\u56fe\u5c42<\/strong><\/li>\n<\/ol>\n<p>\u5728ggplot\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>aes()<\/code>\u51fd\u6570\u6765\u6307\u5b9a\u8981\u6dfb\u52a0\u56fe\u4f8b\u7684\u56fe\u5c42\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u5e0c\u671b\u56fe\u4f8b\u663e\u793a\u4e0d\u540c\u989c\u8272\u7684\u6570\u636e\u70b9\uff0c\u53ef\u4ee5\u5728<code>aes()<\/code>\u51fd\u6570\u4e2d\u4f7f\u7528<code>color<\/code>\u53c2\u6570\u3002<\/p>\n<ol start=\"2\">\n<li><strong>\u5728<code>scale_<\/code>\u51fd\u6570\u4e2d\u8bbe\u7f6e\u56fe\u4f8b\u5c5e\u6027<\/strong><\/li>\n<\/ol>\n<p>\u901a\u8fc7\u4f7f\u7528<code>scale_<\/code>\u51fd\u6570\uff0c\u60a8\u53ef\u4ee5\u8bbe\u7f6e\u56fe\u4f8b\u7684\u5404\u79cd\u5c5e\u6027\uff0c\u5982\u6807\u9898\u3001\u6807\u7b7e\u548c\u4f4d\u7f6e\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>scale_color_manual()<\/code>\u51fd\u6570\u53ef\u4ee5\u624b\u52a8\u8bbe\u7f6e\u989c\u8272\u56fe\u4f8b\u7684\u5c5e\u6027\uff0c\u5982\u6807\u9898\u548c\u6807\u7b7e\u3002<\/p>\n<ol start=\"3\">\n<li><strong>\u4f7f\u7528<code>theme()<\/code>\u51fd\u6570\u81ea\u5b9a\u4e49\u56fe\u4f8b\u6837\u5f0f<\/strong><\/li>\n<\/ol>\n<p>\u4f7f\u7528<code>theme()<\/code>\u51fd\u6570\u53ef\u4ee5\u5bf9\u56fe\u4f8b\u8fdb\u884c\u81ea\u5b9a\u4e49\u6837\u5f0f\u8bbe\u7f6e\u3002\u60a8\u53ef\u4ee5\u66f4\u6539\u56fe\u4f8b\u7684\u5b57\u4f53\u3001\u5927\u5c0f\u3001\u6807\u7b7e\u4f4d\u7f6e\u7b49\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>legend.position<\/code>\u53c2\u6570\u53ef\u4ee5\u8bbe\u7f6e\u56fe\u4f8b\u7684\u4f4d\u7f6e\u3002<\/p>\n<p>\u8bf7\u6ce8\u610f\uff0c\u5728\u4f7f\u7528ggplot\u6dfb\u52a0\u56fe\u4f8b\u65f6\uff0c\u786e\u4fdd\u5c06\u56fe\u4f8b\u76f8\u5173\u7684\u53c2\u6570\u548c\u51fd\u6570\u5e94\u7528\u4e8e\u6b63\u786e\u7684\u56fe\u5c42\u548c\u6620\u5c04\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728R\u8bed\u8a00\u7684ggplot\u4e2d\u663e\u793a\u56fe\u4f8b\u6807\u9898\uff1f<\/strong><\/p>\n<p>\u8981\u5728ggplot\u56fe\u4e2d\u663e\u793a\u56fe\u4f8b\u6807\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528<code>labs()<\/code>\u51fd\u6570\u3002\u8be5\u51fd\u6570\u53ef\u7528\u4e8e\u6dfb\u52a0\u56fe\u4f8b\u7684\u6807\u9898\uff0c\u5e76\u8bbe\u7f6e\u56fe\u4f8b\u7684\u5c5e\u6027\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>labs(color = &quot;\u6807\u9898&quot;)<\/code>\uff0c\u60a8\u53ef\u4ee5\u4e3a\u989c\u8272\u56fe\u4f8b\u6307\u5b9a\u6807\u9898\u4e3a\u201c\u6807\u9898\u201d\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4f7f\u7528<code>labs()<\/code>\u51fd\u6570\u65f6\uff0c\u9700\u8981\u6307\u5b9a\u56fe\u4f8b\u7684\u5c5e\u6027\u540d\u79f0\uff0c\u5982<code>color<\/code>\u3001<code>shape<\/code>\u3001<code>fill<\/code>\u7b49\u3002\u786e\u4fdd\u5c06<code>labs()<\/code>\u51fd\u6570\u5e94\u7528\u4e8e\u6b63\u786e\u7684\u56fe\u5c42\u3002<\/p>\n<p><strong>\u6211\u5982\u4f55\u5728ggplot\u4e2d\u81ea\u5b9a\u4e49\u56fe\u4f8b\u6807\u7b7e\uff1f<\/strong><\/p>\n<p>\u8981\u5728ggplot\u4e2d\u81ea\u5b9a\u4e49\u56fe\u4f8b\u6807\u7b7e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>scale_<\/code>\u51fd\u6570\u3002\u901a\u8fc7\u4f7f\u7528<code>scale_<\/code>\u51fd\u6570\uff0c\u60a8\u53ef\u4ee5\u624b\u52a8\u8bbe\u7f6e\u56fe\u4f8b\u6807\u7b7e\u7684\u503c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>scale_color_manual()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u6307\u5b9a\u6bcf\u4e2a\u989c\u8272\u7684\u5bf9\u5e94\u6807\u7b7e\u503c\u3002<\/p>\n<p>\u4f7f\u7528<code>values<\/code>\u53c2\u6570\uff0c\u60a8\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u6807\u7b7e\u6307\u5b9a\u65b0\u7684\u503c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>values=c(&quot;\u6807\u7b7e1&quot;, &quot;\u6807\u7b7e2&quot;, &quot;\u6807\u7b7e3&quot;)<\/code>\uff0c\u53ef\u4ee5\u5c06\u989c\u8272\u56fe\u4f8b\u6807\u7b7e\u8bbe\u7f6e\u4e3a\u81ea\u5b9a\u4e49\u7684\u503c\u3002<\/p>\n<p>\u786e\u4fdd\u5728\u4f7f\u7528<code>scale_<\/code>\u51fd\u6570\u65f6\u5e94\u7528\u4e8e\u6b63\u786e\u7684\u56fe\u5c42\uff0c\u5e76\u6307\u5b9a\u6b63\u786e\u7684\u5c5e\u6027\u540d\u79f0\uff0c\u5982<code>color<\/code>\u3001<code>shape<\/code>\u3001<code>fill<\/code>\u7b49\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"R\u8bed\u8a00\u7684ggplot2\u5305\u4e2d\u6dfb\u52a0\u56fe\u4f8b\u901a\u5e38\u662f\u81ea\u52a8\u751f\u6210\u7684\uff0c\u5f53\u4f60\u6620\u5c04\u4e86\u4e00\u4e2a aesthetic\uff08\u5982\u989c\u8272\u3001\u5f62\u72b6\u3001\u5927\u5c0f\u7b49\uff09 [&hellip;]","protected":false},"author":3,"featured_media":294138,"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\/294121"}],"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=294121"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/294121\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/294138"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=294121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=294121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=294121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}