{"id":419,"date":"2023-04-03T02:21:23","date_gmt":"2023-04-03T02:21:23","guid":{"rendered":"https:\/\/yunshangtulv.com.cn\/?p=419"},"modified":"2023-04-03T02:21:23","modified_gmt":"2023-04-03T02:21:23","slug":"r%e8%af%ad%e8%a8%80%e5%9f%ba%e7%a1%80%e7%bb%98%e5%9b%be%ef%bc%9a%e9%a5%bc%e7%8a%b6%e5%9b%be","status":"publish","type":"post","link":"https:\/\/yunshangtulv.com.cn\/?p=419","title":{"rendered":"R\u8bed\u8a00\u57fa\u7840\u7ed8\u56fe\uff1a\u997c\u72b6\u56fe"},"content":{"rendered":"<p>\u997c\u72b6\u56fe\u662f\u4e00\u79cd\u53ef\u89c6\u5316\u5de5\u5177\uff0c\u7528\u4e8e\u663e\u793a\u7c7b\u522b\u53d8\u91cf\u7684\u76f8\u5bf9\u6bd4\u4f8b\u3002\u5728R\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ggplot2<\/code>\u5305\u521b\u5efa\u997c\u72b6\u56fe\u3002\u4ee5\u4e0b\u662f\u997c\u72b6\u56fe\u7684\u8be6\u89e3\u548c\u5e94\u7528\u5b9e\u4f8b\uff1a<\/p>\n<ol>\n<li>\u5b89\u88c5\u5e76\u52a0\u8f7d<code>ggplot2<\/code>\u5305\uff1a<\/li>\n<\/ol>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md\"><\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-R\"><span class=\"hljs-comment\"># \u5982\u679c\u5c1a\u672a\u5b89\u88c5\uff0c\u8bf7\u5b89\u88c5ggplot2\u5305<\/span><br \/>\n<span class=\"hljs-keyword\">if<\/span> <span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-operator\">!<\/span>requireNamespace<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-string\">\"ggplot2\"<\/span><span class=\"hljs-punctuation\">,<\/span> quietly <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-literal\">TRUE<\/span><span class=\"hljs-punctuation\">)<\/span><span class=\"hljs-punctuation\">)<\/span><br \/>\n    install.packages<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-string\">\"ggplot2\"<\/span><span class=\"hljs-punctuation\">)<\/span><\/p>\n<p><span class=\"hljs-comment\"># \u52a0\u8f7dggplot2\u5305<\/span><br \/>\nlibrary<span class=\"hljs-punctuation\">(<\/span>ggplot2<span class=\"hljs-punctuation\">)<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<ol start=\"2\">\n<li>\u51c6\u5907\u793a\u4f8b\u6570\u636e\uff1a<\/li>\n<\/ol>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md\"><\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-R\"><span class=\"hljs-comment\"># \u521b\u5efa\u4e00\u4e2a\u6570\u636e\u6846\uff0c\u5305\u542b\u7c7b\u522b\uff08Category\uff09\u548c\u5bf9\u5e94\u7684\u8ba1\u6570\uff08Count\uff09<\/span><br \/>\ndata <span class=\"hljs-operator\">&lt;-<\/span> data.frame<span class=\"hljs-punctuation\">(<\/span><br \/>\n  Category <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-built_in\">c<\/span><span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-string\">\"A\"<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-string\">\"B\"<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-string\">\"C\"<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-string\">\"D\"<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-string\">\"E\"<\/span><span class=\"hljs-punctuation\">)<\/span><span class=\"hljs-punctuation\">,<\/span><br \/>\n  Count <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-built_in\">c<\/span><span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-number\">25<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-number\">35<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-number\">30<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-number\">40<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-number\">20<\/span><span class=\"hljs-punctuation\">)<\/span><br \/>\n<span class=\"hljs-punctuation\">)<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<ol start=\"3\">\n<li>\u4f7f\u7528<code>ggplot2<\/code>\u521b\u5efa\u997c\u72b6\u56fe\uff1a<\/li>\n<\/ol>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md\"><\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-R\"><span class=\"hljs-comment\"># \u8ba1\u7b97\u6bcf\u4e2a\u7c7b\u522b\u7684\u6bd4\u4f8b<\/span><br \/>\ndata<span class=\"hljs-operator\">$<\/span>Percentage <span class=\"hljs-operator\">&lt;-<\/span> data<span class=\"hljs-operator\">$<\/span>Count <span class=\"hljs-operator\">\/<\/span> <span class=\"hljs-built_in\">sum<\/span><span class=\"hljs-punctuation\">(<\/span>data<span class=\"hljs-operator\">$<\/span>Count<span class=\"hljs-punctuation\">)<\/span><\/p>\n<p><span class=\"hljs-comment\"># \u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u957f\u683c\u5f0f<\/span><br \/>\ndata_long <span class=\"hljs-operator\">&lt;-<\/span> tidyr<span class=\"hljs-operator\">::<\/span>gather<span class=\"hljs-punctuation\">(<\/span>data<span class=\"hljs-punctuation\">,<\/span> key <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">\"Variable\"<\/span><span class=\"hljs-punctuation\">,<\/span> value <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">\"Value\"<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-operator\">-<\/span>Category<span class=\"hljs-punctuation\">)<\/span><\/p>\n<p><span class=\"hljs-comment\"># \u521b\u5efa\u997c\u72b6\u56fe<\/span><br \/>\npie_chart <span class=\"hljs-operator\">&lt;-<\/span> ggplot<span class=\"hljs-punctuation\">(<\/span>data_long<span class=\"hljs-punctuation\">,<\/span> aes<span class=\"hljs-punctuation\">(<\/span>x <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">\"\"<\/span><span class=\"hljs-punctuation\">,<\/span> y <span class=\"hljs-operator\">=<\/span> Value<span class=\"hljs-punctuation\">,<\/span> fill <span class=\"hljs-operator\">=<\/span> Category<span class=\"hljs-punctuation\">)<\/span><span class=\"hljs-punctuation\">)<\/span> <span class=\"hljs-operator\">+<\/span><br \/>\n  geom_bar<span class=\"hljs-punctuation\">(<\/span>stat <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">\"identity\"<\/span><span class=\"hljs-punctuation\">,<\/span> width <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-number\">1<\/span><span class=\"hljs-punctuation\">)<\/span> <span class=\"hljs-operator\">+<\/span><br \/>\n  coord_polar<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-string\">\"y\"<\/span><span class=\"hljs-punctuation\">,<\/span> start <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-number\">0<\/span><span class=\"hljs-punctuation\">)<\/span> <span class=\"hljs-operator\">+<\/span><br \/>\n  theme_void<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-punctuation\">)<\/span> <span class=\"hljs-operator\">+<\/span><br \/>\n  theme<span class=\"hljs-punctuation\">(<\/span>legend.position <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">\"bottom\"<\/span><span class=\"hljs-punctuation\">)<\/span> <span class=\"hljs-operator\">+<\/span><br \/>\n  labs<span class=\"hljs-punctuation\">(<\/span>fill <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">\"Category\"<\/span><span class=\"hljs-punctuation\">,<\/span> title <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">\"Pie Chart Example\"<\/span><span class=\"hljs-punctuation\">)<\/span><\/p>\n<p><span class=\"hljs-comment\"># \u7ed8\u5236\u997c\u72b6\u56fe<\/span><br \/>\nprint<span class=\"hljs-punctuation\">(<\/span>pie_chart<span class=\"hljs-punctuation\">)<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u7c7b\u522b\u548c\u8ba1\u6570\u7684\u6570\u636e\u6846\u3002\u63a5\u7740\uff0c\u6211\u4eec\u8ba1\u7b97\u6bcf\u4e2a\u7c7b\u522b\u7684\u6bd4\u4f8b\uff0c\u5e76\u4f7f\u7528<code>ggplot2<\/code>\u5305\u521b\u5efa\u997c\u72b6\u56fe\u3002<code>geom_bar<\/code>\u51fd\u6570\u7528\u4e8e\u7ed8\u5236\u6761\u5f62\u56fe\uff0c\u800c<code>coord_polar<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u6781\u5750\u6807\u7cfb\uff0c\u4ece\u800c\u521b\u5efa\u997c\u72b6\u56fe\u3002\u6211\u4eec\u8fd8\u4f7f\u7528\u4e86\u4e00\u4e9b\u5176\u4ed6\u8bbe\u7f6e\uff0c\u4f8b\u5982<code>theme_void<\/code>\u548c<code>legend.position<\/code>\uff0c\u4ee5\u4f18\u5316\u56fe\u5f62\u7684\u5916\u89c2\u3002<\/p>\n<p>\u8bf7\u6ce8\u610f\uff0c\u5c3d\u7ba1\u997c\u72b6\u56fe\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u6709\u7528\uff0c\u4f46\u5b83\u4eec\u901a\u5e38\u4e0d\u662f\u6700\u4f73\u7684\u53ef\u89c6\u5316\u9009\u62e9\uff0c\u56e0\u4e3a\u4eba\u773c\u5f88\u96be\u51c6\u786e\u6bd4\u8f83\u6247\u5f62\u7684\u5927\u5c0f\u3002\u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\uff0c\u6761\u5f62\u56fe\u6216\u5806\u53e0\u6761\u5f62\u56fe\u53ef\u80fd\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u997c\u72b6\u56fe\u662f\u4e00\u79cd\u53ef\u89c6\u5316\u5de5\u5177\uff0c\u7528\u4e8e\u663e\u793a\u7c7b\u522b\u53d8\u91cf\u7684\u76f8\u5bf9\u6bd4\u4f8b\u3002\u5728R\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528ggplot2\u5305\u521b\u5efa\u997c\u72b6\u56fe\u3002\u4ee5\u4e0b\u662f\u997c\u72b6 [&hellip;]<\/p>\n","protected":false},"author":111,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-419","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/users\/111"}],"replies":[{"embeddable":true,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=419"}],"version-history":[{"count":0,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/419\/revisions"}],"wp:attachment":[{"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}