{"id":5614,"date":"2025-05-19T22:43:32","date_gmt":"2025-05-19T14:43:32","guid":{"rendered":"http:\/\/192.168.1.29\/?p=5614"},"modified":"2025-05-19T22:44:08","modified_gmt":"2025-05-19T14:44:08","slug":"5614","status":"publish","type":"post","link":"http:\/\/g1n29wqq.ipyingshe.net:5347\/?p=5614","title":{"rendered":"R\u8bed\u8a00\u6570\u636e\u53ef\u89c6\u5316"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code><strong>R note book \u6587\u6863--\u8f93\u51fahtml\u683c\u5f0f\u6587\u6863\uff0cplotly\u4e0d\u80fd\u751f\u6210PDF\u6587\u4ef6<\/strong>\n---\ntitle: \"R\u8bed\u8a00\u6570\u636e\u53ef\u89c6\u5316\"\noutput: html_notebook\n---\n\u5728R\u8bed\u8a00\u4e2d\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u662f\u6570\u636e\u5206\u6790\u548c\u5448\u73b0\u7684\u91cd\u8981\u73af\u8282\uff0cR\u63d0\u4f9b\u4e86\u591a\u79cd\u5f3a\u5927\u7684\u7ed8\u56fe\u7cfb\u7edf\u548c\u5de5\u5177\u3002\u4ee5\u4e0b\u662f\u5e38\u89c1\u7684\u6570\u636e\u53ef\u89c6\u5316\u65b9\u6cd5\u548c\u793a\u4f8b\uff0c\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u9010\u6b65\u4ecb\u7ecd\uff1a\n\n### **1. \u57fa\u7840\u7ed8\u56fe\u7cfb\u7edf\uff08Base R Graphics\uff09**\nR\u5185\u7f6e\u7684\u57fa\u7840\u7ed8\u56fe\u51fd\u6570\uff0c\u9002\u5408\u5feb\u901f\u751f\u6210\u7b80\u5355\u56fe\u8868\u3002\n\n#### **\u6563\u70b9\u56fe**\n```{r}\n# \u793a\u4f8b\u6570\u636e\nx &lt;- 1:10\ny &lt;- x^2\n\n# \u7ed8\u5236\u6563\u70b9\u56fe\nplot(x, y, \n     main = \"\u6563\u70b9\u56fe\u793a\u4f8b\",  # \u6807\u9898\n     xlab = \"X\u8f74\", ylab = \"Y\u8f74\",  # \u5750\u6807\u8f74\u6807\u7b7e\n     pch = 16,  # \u70b9\u7684\u5f62\u72b6\n     col = \"blue\",  # \u70b9\u7684\u989c\u8272\n     cex = 1.5)  # \u70b9\u7684\u5927\u5c0f\n```\n\n#### **\u6298\u7ebf\u56fe**\n```{r}\n# \u751f\u6210\u6570\u636e\ntime &lt;- seq(1, 10, by = 0.5)\nvalues &lt;- sin(time)\n\n# \u7ed8\u5236\u6298\u7ebf\u56fe\nplot(time, values, type = \"l\",  # type=\"l\"\u8868\u793a\u6298\u7ebf\u56fe\n     main = \"\u6b63\u5f26\u66f2\u7ebf\",\n     xlab = \"\u65f6\u95f4\", ylab = \"\u503c\",\n     col = \"red\", lwd = 2)  # \u7ebf\u5bbd\u4e3a2\n```\n\n#### **\u7bb1\u7ebf\u56fe**\n```{r}\n# \u4f7f\u7528\u5185\u7f6e\u6570\u636e\u96c6mtcars\ndata(mtcars)\n\n# \u6309\u6c14\u7f38\u6570(cyl)\u5206\u7ec4\u7ed8\u5236mpg\u7684\u7bb1\u7ebf\u56fe\nboxplot(mpg ~ cyl, data = mtcars,\n        main = \"\u4e0d\u540c\u6c14\u7f38\u6570\u8f66\u8f86\u7684MPG\u5206\u5e03\",\n        xlab = \"\u6c14\u7f38\u6570\", ylab = \"\u6bcf\u52a0\u4ed1\u82f1\u91cc\u6570(MPG)\",\n        col = c(\"lightblue\", \"lightgreen\", \"lightpink\"))\n```\n\n\n### **2. ggplot2\u5305\uff08\u63a8\u8350\uff09**\n\u57fa\u4e8e\u56fe\u5f62\u8bed\u6cd5\u7684\u9ad8\u7ea7\u7ed8\u56fe\u7cfb\u7edf\uff0c\u9002\u5408\u521b\u5efa\u590d\u6742\u3001\u7cbe\u7f8e\u7684\u56fe\u8868\u3002\n\n#### **\u5b89\u88c5\u4e0e\u52a0\u8f7d**\n```{r}\n#install.packages(\"ggplot2\")  # \u9996\u6b21\u4f7f\u7528\u9700\u5b89\u88c5\nlibrary(ggplot2)\n```\n\n#### **\u6563\u70b9\u56fe**\n```{r}\n# \u4f7f\u7528\u5185\u7f6e\u6570\u636e\u96c6iris\nggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +\n  geom_point(size = 3) +  # \u6563\u70b9\u56fe\u5c42\n  labs(title = \"\u9e22\u5c3e\u82b1\u843c\u7247\u957f\u5ea6\u4e0e\u5bbd\u5ea6\u5173\u7cfb\",\n       x = \"\u843c\u7247\u957f\u5ea6(cm)\", y = \"\u843c\u7247\u5bbd\u5ea6(cm)\") +\n  theme_minimal()  # \u4f7f\u7528\u7b80\u6d01\u4e3b\u9898\n```\n\n#### **\u67f1\u72b6\u56fe**\n```{r}\n# \u7edf\u8ba1\u4e0d\u540cSpecies\u7684\u6570\u91cf\nggplot(iris, aes(x = Species)) +\n  geom_bar(fill = \"skyblue\", color = \"black\") +  # \u67f1\u72b6\u56fe\u5c42\n  labs(title = \"\u9e22\u5c3e\u82b1\u79cd\u7c7b\u5206\u5e03\", x = \"\u79cd\u7c7b\", y = \"\u6570\u91cf\") +\n  theme_classic()  # \u4f7f\u7528\u7ecf\u5178\u4e3b\u9898\n```\n\n#### **\u7bb1\u7ebf\u56fe\u4e0e\u5c0f\u63d0\u7434\u56fe**\n```{r}\nggplot(iris, aes(x = Species, y = Petal.Length, fill = Species)) +\n  geom_boxplot(alpha = 0.7) +  # \u7bb1\u7ebf\u56fe\u5c42\n  geom_violin(alpha = 0.3) +   # \u5c0f\u63d0\u7434\u56fe\u5c42\uff08\u663e\u793a\u5bc6\u5ea6\u5206\u5e03\uff09\n  labs(title = \"\u4e0d\u540c\u79cd\u7c7b\u9e22\u5c3e\u82b1\u82b1\u74e3\u957f\u5ea6\u5206\u5e03\",\n       x = \"\u79cd\u7c7b\", y = \"\u82b1\u74e3\u957f\u5ea6(cm)\")\n```\n\n\n### **3. \u9ad8\u7ea7\u53ef\u89c6\u5316**\n\n#### **\u70ed\u56fe\uff08Heatmap\uff09**\n```{r}\n# \u4f7f\u7528\u5185\u7f6e\u6570\u636e\u96c6mtcars\ncor_matrix &lt;- cor(mtcars)  # \u8ba1\u7b97\u76f8\u5173\u7cfb\u6570\u77e9\u9635\n\n# \u7ed8\u5236\u70ed\u56fe\nheatmap(cor_matrix, \n        main = \"\u6c7d\u8f66\u7279\u5f81\u76f8\u5173\u6027\u70ed\u56fe\",\n        col = cm.colors(256),  # \u989c\u8272\u6e10\u53d8\n        scale = \"column\")  # \u6309\u5217\u6807\u51c6\u5316\n```\n\n#### **\u76f4\u65b9\u56fe\u4e0e\u5bc6\u5ea6\u56fe**\n```{r}\n# \u4f7f\u7528ggplot2\u7ed8\u5236\u76f4\u65b9\u56fe\u4e0e\u5bc6\u5ea6\u56fe\nggplot(iris, aes(x = Sepal.Length)) +\n  geom_histogram(aes(y = ..density..),  # \u76f4\u65b9\u56fe\uff08\u663e\u793a\u5bc6\u5ea6\uff09\n                 bins = 15, \n                 fill = \"lightblue\", \n                 color = \"black\") +\n  geom_density(alpha = 0.2, fill = \"blue\") +  # \u5bc6\u5ea6\u66f2\u7ebf\n  facet_wrap(~ Species) +  # \u6309\u79cd\u7c7b\u5206\u9762\n  labs(title = \"\u9e22\u5c3e\u82b1\u843c\u7247\u957f\u5ea6\u5206\u5e03\", x = \"\u843c\u7247\u957f\u5ea6(cm)\", y = \"\u5bc6\u5ea6\")\n```\n\n#### **\u6c14\u6ce1\u56fe\uff08\u6563\u70b9\u56fe+\u5927\u5c0f\/\u989c\u8272\u6620\u5c04\uff09**\n```{r}\n# \u4f7f\u7528ggplot2\u7ed8\u5236\u6c14\u6ce1\u56fe\nggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +\n  geom_point(aes(size = Petal.Length, color = Species), \n             alpha = 0.7) +  # alpha\u8bbe\u7f6e\u900f\u660e\u5ea6\n  scale_size_continuous(range = c(2, 10)) +  # \u63a7\u5236\u70b9\u5927\u5c0f\u8303\u56f4\n  labs(title = \"\u9e22\u5c3e\u82b1\u7279\u5f81\u6c14\u6ce1\u56fe\",\n       x = \"\u843c\u7247\u957f\u5ea6(cm)\", y = \"\u843c\u7247\u5bbd\u5ea6(cm)\",\n       size = \"\u82b1\u74e3\u957f\u5ea6\", color = \"\u79cd\u7c7b\") +\n  theme_bw()  # \u4f7f\u7528\u9ed1\u767d\u4e3b\u9898\n```\n\n\n### **4. \u4ea4\u4e92\u5f0f\u53ef\u89c6\u5316\uff08plotly\uff09**\n\u521b\u5efa\u53ef\u4ea4\u4e92\u7684\u56fe\u8868\uff0c\u9002\u5408\u7f51\u9875\u5c55\u793a\u6216\u6570\u636e\u5206\u6790\u3002\n\n#### **\u5b89\u88c5\u4e0e\u57fa\u672c\u7528\u6cd5**\n```{r}\n#install.packages(\"plotly\")\nlibrary(plotly)\n\n# \u5c06ggplot2\u56fe\u8868\u8f6c\u4e3a\u4ea4\u4e92\u5f0f\np &lt;- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +\n  geom_point(size = 3)\n\nggplotly(p)  # \u8f6c\u4e3a\u4ea4\u4e92\u5f0f\u56fe\u8868\n```\n\n#### **3D\u6563\u70b9\u56fe**\n```{r}\n# \u521b\u5efa3D\u6563\u70b9\u56fe\nplot_ly(iris, x = ~Sepal.Length, y = ~Sepal.Width, z = ~Petal.Length,\n        color = ~Species, type = \"scatter3d\", mode = \"markers\") %>%\n  layout(title = \"\u9e22\u5c3e\u82b13D\u7279\u5f81\u6563\u70b9\u56fe\")\n```\n\n\n### **5. \u6570\u636e\u53ef\u89c6\u5316\u6ce8\u610f\u4e8b\u9879**\n1. **\u9009\u62e9\u5408\u9002\u7684\u56fe\u8868\u7c7b\u578b**\uff1a\u6839\u636e\u6570\u636e\u7c7b\u578b\u548c\u5206\u6790\u76ee\u7684\u9009\u62e9\uff08\u5982\u6bd4\u8f83\u7528\u67f1\u72b6\u56fe\uff0c\u5206\u5e03\u7528\u7bb1\u7ebf\u56fe\uff0c\u5173\u7cfb\u7528\u6563\u70b9\u56fe\uff09\u3002\n2. **\u4fdd\u6301\u7b80\u6d01**\uff1a\u907f\u514d\u8fc7\u591a\u88c5\u9970\u5143\u7d20\uff0c\u786e\u4fdd\u6570\u636e\u662f\u7126\u70b9\u3002\n3. **\u4f7f\u7528\u9002\u5f53\u7684\u989c\u8272**\uff1a\u907f\u514d\u4f7f\u7528\u8fc7\u4e8e\u9c9c\u8273\u6216\u96be\u4ee5\u533a\u5206\u7684\u989c\u8272\uff0c\u8003\u8651\u8272\u76f2\u7528\u6237\u3002\n4. **\u6dfb\u52a0\u5fc5\u8981\u6807\u7b7e**\uff1a\u786e\u4fdd\u56fe\u8868\u6807\u9898\u3001\u5750\u6807\u8f74\u6807\u7b7e\u3001\u56fe\u4f8b\u6e05\u6670\u3002\n5. **\u4f18\u5316\u5e03\u5c40**\uff1a\u5408\u7406\u5b89\u6392\u56fe\u8868\u5927\u5c0f\u3001\u6bd4\u4f8b\u548c\u95f4\u8ddd\u3002\n\n\n### **6. \u8d44\u6e90\u63a8\u8350**\n- **ggplot2\u5b98\u65b9\u6587\u6863**\uff1ahttps:\/\/ggplot2.tidyverse.org\/\n- **R Graphics Cookbook**\uff1ahttps:\/\/r-graphics.org\/\n- **Plotly for R**\uff1ahttps:\/\/plotly.com\/r\/\n\n\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5728R\u4e2d\u521b\u5efa\u4ece\u7b80\u5355\u5230\u590d\u6742\u7684\u5404\u79cd\u6570\u636e\u53ef\u89c6\u5316\u56fe\u8868\uff0c\u6ee1\u8db3\u4e0d\u540c\u7684\u5206\u6790\u548c\u5c55\u793a\u9700\u6c42\u3002<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-5614","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/5614","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5614"}],"version-history":[{"count":2,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/5614\/revisions"}],"predecessor-version":[{"id":5616,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/5614\/revisions\/5616"}],"wp:attachment":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5614"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}