{"id":180538,"date":"2024-05-08T20:37:16","date_gmt":"2024-05-08T12:37:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/180538.html"},"modified":"2024-05-08T20:37:21","modified_gmt":"2024-05-08T12:37:21","slug":"%e5%a6%82%e4%bd%95%e7%94%a8r%e8%af%ad%e8%a8%80%e7%94%bb%e5%b9%bf%e4%b8%9c%e7%9c%81%e5%9c%b0%e5%9b%be%e5%88%92%e5%88%86%e5%87%ba21%e4%b8%aa%e5%9c%b0%e7%ba%a7%e5%b8%82%e7%9a%84%e8%be%b9%e7%95%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/180538.html","title":{"rendered":"\u5982\u4f55\u7528R\u8bed\u8a00\u753b\u5e7f\u4e1c\u7701\u5730\u56fe(\u5212\u5206\u51fa21\u4e2a\u5730\u7ea7\u5e02\u7684\u8fb9\u754c)"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27065814\/cebc1815-545c-40fe-aec8-c54007805102.webp\" alt=\"\u5982\u4f55\u7528R\u8bed\u8a00\u753b\u5e7f\u4e1c\u7701\u5730\u56fe(\u5212\u5206\u51fa21\u4e2a\u5730\u7ea7\u5e02\u7684\u8fb9\u754c)\" \/><\/p>\n<p><p>\u5e7f\u4e1c\u7701\u5730\u56fe\u7684\u7ed8\u5236\u53ef\u4ee5\u901a\u8fc7R\u8bed\u8a00\u4e2d\u7684\u5305\u5982<code>ggplot2<\/code>\u3001<code>sf<\/code>\u7b49\u6765\u5b9e\u73b0\u3002<strong>R\u8bed\u8a00\u7ed8\u5236\u5e7f\u4e1c\u7701\u5730\u884c\u653f\u533a\u5212\u56fe<\/strong>\u6d89\u53ca\u5230\u6570\u636e\u83b7\u53d6\u3001\u5730\u56fe\u7ed8\u5236\u548c\u7f8e\u5316\u4e09\u4e2a\u4e3b\u8981\u73af\u8282\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u83b7\u53d6\u5305\u542b\u5e7f\u4e1c\u770121\u4e2a\u5730\u7ea7\u5e02\u8fb9\u754c\u4fe1\u606f\u7684\u5730\u7406\u6570\u636e\uff0c\u901a\u5e38\u8fd9\u7c7b\u6570\u636e\u662f\u77e2\u91cf\u5f62\u5f0f\u7684Shapefile\uff08.shp\uff09\u6587\u4ef6\u3002\u4e4b\u540e\uff0c\u4f7f\u7528R\u8bed\u8a00\u4e2d\u7684\u76f8\u5173\u5305\u8bfb\u53d6\u8fd9\u4e9b\u5730\u7406\u6570\u636e\uff0c\u5e76\u5229\u7528<code>ggplot2<\/code>\u5bf9\u5176\u8fdb\u884c\u7ed8\u5236\u3002\u7ed8\u56fe\u65f6\uff0c\u8fd8\u53ef\u4ee5\u5e94\u7528\u4e00\u4e9b\u7f8e\u5316\u63aa\u65bd\u4f7f\u5730\u56fe\u66f4\u52a0\u6e05\u6670\u548c\u4e13\u4e1a\uff0c\u4f8b\u5982\u8c03\u6574\u989c\u8272\u3001\u6dfb\u52a0\u6bd4\u4f8b\u5c3a\u548c\u6307\u5317\u9488\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\u4e00\u4e2a\u73af\u8282\uff1a\u6570\u636e\u83b7\u53d6<\/strong>\u3002\u5e7f\u4e1c\u7701\u5404\u5730\u7ea7\u5e02\u8fb9\u754c\u7684\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u5f00\u653e\u7684\u5730\u7406\u4fe1\u606f\u7cfb\u7edf\uff08GIS\uff09\u6570\u636e\u5e93\u83b7\u53d6\uff0c\u4f8b\u5982\u4e2d\u56fd\u56fd\u5bb6\u6d4b\u7ed8\u5730\u7406\u4fe1\u606f\u5c40\u63d0\u4f9b\u7684\u6570\u636e\uff0c\u6216\u8005\u5229\u7528OpenStreetMap\u63d0\u4f9b\u7684\u5f00\u6e90\u6570\u636e\u3002\u4f60\u4e5f\u53ef\u4ee5\u9009\u62e9\u4e00\u4e9b\u4e13\u4e1a\u7684\u5730\u7406\u6570\u636e\u63d0\u4f9b\u5e73\u53f0\uff0c\u5982Natural Earth\u6216GADM\uff0c\u8fd9\u4e9b\u5e73\u53f0\u4e0a\u901a\u5e38\u5305\u542b\u591a\u79cd\u5206\u8fa8\u7387\u7684Shapefile\u6570\u636e\u3002\u4e0b\u8f7d\u5f97\u5230\u7684Shapefile\u6570\u636e\u901a\u5e38\u5305\u62ec\u591a\u4e2a\u6587\u4ef6\u7ec4\u6210\u7684\u6570\u636e\u96c6\u5408\uff0c<code>.shp<\/code>\u6587\u4ef6\u4fdd\u5b58\u7740\u5b9e\u9645\u7684\u51e0\u4f55\u4fe1\u606f\uff0c\u5176\u4ed6\u5982<code>.dbf<\/code>\u3001<code>.shx<\/code>\u6587\u4ef6\u5219\u5305\u542b\u7740\u4e0e\u51e0\u4f55\u4fe1\u606f\u5bf9\u5e94\u7684\u5c5e\u6027\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6570\u636e\u83b7\u53d6\u4e0e\u9884\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u83b7\u53d6\u5e7f\u4e1c\u7701\u5730\u56fe\u6570\u636e\uff0c\u901a\u5e38\u662f\u901a\u8fc7\u7f51\u4e0a\u516c\u5f00\u7684\u5730\u7406\u6570\u636e\u8d44\u6e90\uff0c\u5982GADM\u6570\u636e\u5e93\u7b49\uff0c\u4e0b\u8f7d\u5177\u4f53\u7684Shapefile\u6587\u4ef6\u3002\u4e0b\u8f7d\u540e\u901a\u5e38\u662fZIP\u538b\u7f29\u5305\uff0c\u9700\u8981\u89e3\u538b\u3002\u968f\u540e\uff0c\u901a\u8fc7R\u8bed\u8a00\u7684<code>sf<\/code>\u5305\u8bfb\u53d6\u8fd9\u4e9bShapefile\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-R\">library(sf)<\/p>\n<h2><strong>\u8bfb\u53d6Shapefile\u6587\u4ef6<\/strong><\/h2>\n<p>gd_map &lt;- st_read(&#039;path_to_shapefile\/guangdong.shp&#039;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6570\u636e\u9884\u5904\u7406\uff0c\u9700\u8981\u786e\u4fdd\u5730\u56fe\u6570\u636e\u662f\u9488\u5bf9\u5e7f\u4e1c\u7701\u7684\uff0c\u4e14\u51c6\u786e\u8868\u8fbe\u4e8621\u4e2a\u5730\u7ea7\u5e02\u7684\u754c\u7ebf\u3002\u6709\u65f6\u53ef\u80fd\u8fd8\u9700\u8981\u5bf9\u6570\u636e\u7684\u5750\u6807\u7cfb\u8fdb\u884c\u8f6c\u6362\u4ee5\u9002\u914d\u5730\u56fe\u7ed8\u5236\u5e93\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-R\"># \u5982\u679c\u9700\u8981\uff0c\u5bf9\u5750\u6807\u7cfb\u8fdb\u884c\u8f6c\u6362<\/p>\n<p>gd_map &lt;- st_transform(gd_map, crs = 4326)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u7ed8\u5236\u5e7f\u4e1c\u7701\u57fa\u7840\u5730\u56fe<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7<code>ggplot2<\/code>\u6216\u5176\u4ed6\u7c7b\u4f3c\u7684\u5305\u8fdb\u884c\u5730\u56fe\u7ed8\u5236\u3002R\u8bed\u8a00\u7684<code>ggplot2<\/code>\u5305\u5f3a\u5927\u4e14\u7075\u6d3b\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u591a\u5c42\u7ed8\u56fe\u548c\u7efc\u5408\u7f8e\u5b66\u8bbe\u8ba1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-R\">library(ggplot2)<\/p>\n<h2><strong>\u7ed8\u5236\u57fa\u7840\u5730\u56fe<\/strong><\/h2>\n<p>ggplot(data = gd_map) +<\/p>\n<p>  geom_sf() +  # \u6dfb\u52a0\u5730\u56fe\u5c42<\/p>\n<p>  theme_minimal()  # \u4f7f\u7528\u7b80\u7ea6\u4e3b\u9898<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u786e\u4fdd\u5730\u56fe\u7ed8\u5236\u51fa\u6765\u540e21\u4e2a\u5730\u7ea7\u5e02\u7684\u8fb9\u754c\u6e05\u6670\u53ef\u89c1\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6dfb\u52a0\u81ea\u5b9a\u4e49\u6837\u5f0f<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u5730\u56fe\u66f4\u5177\u53ef\u8bfb\u6027\u548c\u4e13\u4e1a\u6027\uff0c\u53ef\u4ee5\u6dfb\u52a0\u81ea\u5b9a\u4e49\u6837\u5f0f\uff0c\u6bd4\u5982\u8bbe\u5b9a\u4e0d\u540c\u5730\u7ea7\u5e02\u7684\u586b\u5145\u989c\u8272\u3001\u8fb9\u754c\u989c\u8272\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-R\"># \u4e3a\u4e0d\u540c\u5730\u7ea7\u5e02\u8bbe\u7f6e\u4e0d\u540c\u7684\u989c\u8272<\/p>\n<p>ggplot(data = gd_map) +<\/p>\n<p>  geom_sf(aes(fill = region)) +  # region\u4e3a\u5730\u7ea7\u5e02\u5bf9\u5e94\u5b57\u6bb5<\/p>\n<p>  scale_fill_viridis_d()  # \u4f7f\u7528viridis\u989c\u8272\u6e10\u53d8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u540c\u65f6\uff0c\u53ef\u4ee5\u8c03\u6574\u56fe\u4f8b\u3001\u6807\u9898\u4ee5\u53ca\u5176\u4ed6ggplot2\u7684\u7f8e\u5b66\u7279\u6027\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u56fe\u8868\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u52a0\u5165\u6807\u7b7e\u548c\u6ce8\u91ca<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u7840\u7684\u5730\u56fe\u7ed8\u5236\uff0c\u8fd8\u53ef\u4ee5\u5728\u5730\u56fe\u4e0a\u6807\u6ce8\u5730\u7ea7\u5e02\u7684\u540d\u79f0\u3001\u6dfb\u52a0\u6307\u5317\u9488\u548c\u6bd4\u4f8b\u5c3a\u7b49\u4fe1\u606f\uff0c\u4f7f\u5730\u56fe\u66f4\u5b8c\u6574\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-R\"># \u5728\u5730\u56fe\u4e0a\u6dfb\u52a0\u5730\u7ea7\u5e02\u540d\u79f0<\/p>\n<p>ggplot(data = gd_map) +<\/p>\n<p>  geom_sf(aes(fill = region)) +<\/p>\n<p>  geom_sf_text(aes(label = region))  # \u6dfb\u52a0\u6587\u672c\u6807\u7b7e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fdd\u6807\u7b7e\u7684\u6e05\u6670\u53ef\u8bfb\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u6587\u672c\u5927\u5c0f\u3001\u989c\u8272\u548c\u4f4d\u7f6e\u8fdb\u884c\u8c03\u6574\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u51fa\u56fe\u548c\u4fdd\u5b58<\/h3>\n<\/p>\n<p><p>\u5f53\u5730\u56fe\u7ed8\u5236\u5b8c\u6210\u5e76\u901a\u8fc7\u9884\u89c8\u786e\u5b9a\u6548\u679c\u7b26\u5408\u8981\u6c42\u540e\uff0c\u53ef\u4ee5\u5229\u7528<code>ggsave()<\/code>\u51fd\u6570\u5c06\u7ed8\u5236\u7684\u5730\u56fe\u4fdd\u5b58\u4e3a\u56fe\u7247\u6216\u5176\u4ed6\u683c\u5f0f\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-R\"># \u4fdd\u5b58\u7ed8\u5236\u597d\u7684\u5730\u56fe\u4e3a\u56fe\u7247<\/p>\n<p>ggsave(&#039;guangdong_map.png&#039;, plot = last_plot(), width = 10, height = 8, dpi = 300)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4fdd\u5b58\u65f6\u53ef\u4ee5\u9009\u62e9\u4e0d\u540c\u7684\u5206\u8fa8\u7387\u548c\u5c3a\u5bf8\uff0c\u4ee5\u9002\u5e94\u4e0d\u540c\u7684\u5c55\u793a\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c<strong>\u4f7f\u7528R\u8bed\u8a00\u7ed8\u5236\u7684\u5e7f\u4e1c\u7701\u5730\u56fe\u4e0d\u4ec5\u5c55\u793a\u4e8621\u4e2a\u5730\u7ea7\u5e02\u7684\u8fb9\u754c<\/strong>\uff0c\u8fd8\u80fd\u901a\u8fc7\u6dfb\u52a0\u4e0d\u540c\u7684\u6837\u5f0f\u548c\u6ce8\u91ca\uff0c\u5c06\u5730\u56fe\u5236\u4f5c\u5f97\u66f4\u522b\u81f4\u3001\u66f4\u6709\u4e2a\u6027\u5316\u3002\u5728\u73b0\u4ee3\u6570\u636e\u53ef\u89c6\u5316\u9886\u57df\uff0c\u8fd9\u7c7b\u6280\u80fd\u8d8a\u6765\u8d8a\u53d7\u5230\u91cd\u89c6\uff0c\u5b83\u5e2e\u52a9\u4eba\u4eec\u66f4\u76f4\u89c2\u5730\u7406\u89e3\u5730\u7406\u4fe1\u606f\u548c\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q1: \u5728R\u8bed\u8a00\u4e2d\u5982\u4f55\u52a0\u8f7d\u5730\u56fe\u6570\u636e\u5e76\u753b\u51fa\u5e7f\u4e1c\u7701\u7684\u5730\u56fe\uff1f<\/strong><\/p>\n<p>\u4f7f\u7528R\u8bed\u8a00\u4e2d\u7684<code>sf<\/code>\u5305\u548c<code>ggplot2<\/code>\u5305\u53ef\u4ee5\u5b9e\u73b0\u52a0\u8f7d\u5730\u56fe\u6570\u636e\u5e76\u753b\u51fa\u5e7f\u4e1c\u7701\u7684\u5730\u56fe\u3002\u9996\u5148\uff0c\u9700\u8981\u4e0b\u8f7d\u5e7f\u4e1c\u7701\u7684\u5730\u7406\u8fb9\u754c\u6570\u636e\uff0c\u53ef\u4ee5\u4ece\u516c\u5f00\u7684\u5730\u7406\u6570\u636e\u7f51\u7ad9\u4e0a\u83b7\u53d6\u6216\u662f\u4f7f\u7528<code>rgdal<\/code>\u5305\u4e2d\u63d0\u4f9b\u7684\u6837\u4f8b\u6570\u636e\u3002\u4e4b\u540e\uff0c\u52a0\u8f7d\u5730\u56fe\u6570\u636e\u5e76\u5229\u7528<code>ggplot2<\/code>\u5305\u4e2d\u7684\u51fd\u6570\u8fdb\u884c\u53ef\u89c6\u5316\u5904\u7406\uff0c\u6700\u540e\u5c31\u53ef\u4ee5\u5f97\u5230\u5e7f\u4e1c\u7701\u7684\u5730\u56fe\u3002<\/p>\n<p><strong>Q2: \u5982\u4f55\u5c06\u5e7f\u4e1c\u7701\u7684\u5730\u56fe\u5212\u5206\u4e3a21\u4e2a\u5730\u7ea7\u5e02\uff0c\u5e76\u6807\u6ce8\u5404\u4e2a\u5730\u7ea7\u5e02\u7684\u540d\u79f0\uff1f<\/strong><\/p>\n<p>\u5728R\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sf<\/code>\u5305\u4e2d\u7684\u51fd\u6570\u5c06\u5e7f\u4e1c\u7701\u7684\u5730\u56fe\u5212\u5206\u4e3a21\u4e2a\u5730\u7ea7\u5e02\u7684\u533a\u57df\u3002\u9996\u5148\uff0c\u9700\u8981\u83b7\u53d6\u5e7f\u4e1c\u7701\u5730\u7406\u8fb9\u754c\u6570\u636e\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a<code>sf<\/code>\u5bf9\u8c61\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>st_centroid<\/code>\u51fd\u6570\u8ba1\u7b97\u6bcf\u4e2a\u5730\u7ea7\u5e02\u7684\u4e2d\u5fc3\u70b9\uff0c\u518d\u7ed3\u5408<code>ggplot2<\/code>\u5305\u4e2d\u7684\u51fd\u6570\u8fdb\u884c\u5730\u56fe\u7684\u7ed8\u5236\uff0c\u5e76\u4f7f\u7528<code>geom_text<\/code>\u51fd\u6570\u6dfb\u52a0\u5730\u7ea7\u5e02\u540d\u79f0\u7684\u6807\u6ce8\u3002<\/p>\n<p><strong>Q3: \u5982\u4f55\u5bf9\u5e7f\u4e1c\u7701\u7684\u5730\u56fe\u8fdb\u884c\u5176\u4ed6\u6837\u5f0f\u7684\u7f8e\u5316\uff1f<\/strong><\/p>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u5730\u56fe\u7ed8\u5236\u5916\uff0c\u8fd8\u53ef\u4ee5\u5bf9\u5e7f\u4e1c\u7701\u7684\u5730\u56fe\u8fdb\u884c\u5176\u4ed6\u6837\u5f0f\u7684\u7f8e\u5316\u3002\u53ef\u4ee5\u8c03\u6574\u5730\u56fe\u7684\u989c\u8272\u3001\u7ebf\u6761\u7c97\u7ec6\u3001\u80cc\u666f\u548c\u6807\u9898\u7b49\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>theme<\/code>\u51fd\u6570\u8c03\u6574\u5730\u56fe\u7684\u80cc\u666f\u989c\u8272\uff0c\u4f7f\u7528<code>scale_fill_manual<\/code>\u51fd\u6570\u8bbe\u7f6e\u5730\u56fe\u533a\u57df\u7684\u586b\u5145\u989c\u8272\uff0c\u4f7f\u7528<code>scale_x_continuous<\/code>\u548c<code>scale_y_continuous<\/code>\u51fd\u6570\u8c03\u6574\u5750\u6807\u8f74\u7684\u8303\u56f4\uff0c\u540c\u65f6\u53ef\u4ee5\u4f7f\u7528<code>labs<\/code>\u51fd\u6570\u6dfb\u52a0\u5730\u56fe\u7684\u6807\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5e7f\u4e1c\u7701\u5730\u56fe\u7684\u7ed8\u5236\u53ef\u4ee5\u901a\u8fc7R\u8bed\u8a00\u4e2d\u7684\u5305\u5982ggplot2\u3001sf\u7b49\u6765\u5b9e\u73b0\u3002R\u8bed\u8a00\u7ed8\u5236\u5e7f\u4e1c\u7701\u5730\u884c\u653f\u533a\u5212\u56fe\u6d89\u53ca\u5230\u6570\u636e\u83b7\u53d6 [&hellip;]","protected":false},"author":3,"featured_media":180546,"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\/180538"}],"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=180538"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/180538\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/180546"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=180538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=180538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=180538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}