{"id":5617,"date":"2025-05-19T23:12:06","date_gmt":"2025-05-19T15:12:06","guid":{"rendered":"http:\/\/192.168.1.29\/?p=5617"},"modified":"2025-05-19T23:12:07","modified_gmt":"2025-05-19T15:12:07","slug":"r%e8%af%ad%e8%a8%80plot%e5%92%8c%e6%a6%82%e7%8e%87%e5%af%86%e5%ba%a6ecdf%e7%bb%98%e5%9b%be%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"http:\/\/g1n29wqq.ipyingshe.net:5347\/?p=5617","title":{"rendered":"R\u8bed\u8a00plot()\u548c\u6982\u7387\u5bc6\u5ea6ecdf()\u7ed8\u56fe\u51fd\u6570"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>---\ntitle: \"R \u8bed\u8a00\u4e2dplot()\u51fd\u6570\u548cecdf()\u51fd\u6570\u7684\u4f7f\u7528\u793a\u4f8b\"\nauthor: \"cnliutz\"\ndate: \"2025-05-18\"\noutput:\n  html_document:\n    df_print: paged\n---\n\u4ee5\u4e0b\u662f R \u8bed\u8a00\u4e2dplot()\u51fd\u6570\u548cecdf()\u51fd\u6570\u7684\u4f7f\u7528\u793a\u4f8b\uff0c\u5305\u542b\u57fa\u7840\u7528\u6cd5\u548c\u53ef\u89c6\u5316\u5c55\u793a\uff1a\n#  1. plot()\u51fd\u6570\u57fa\u7840\u7528\u6cd5\u793a\u4f8b\n```{r}\n# \u793a\u4f8b1: \u7ed8\u5236\u6563\u70b9\u56fe\nx &lt;- 1:10\ny &lt;- x^2\nplot(x, y, main=\"\u6563\u70b9\u56fe\u793a\u4f8b\", xlab=\"X\u8f74\", ylab=\"Y\u8f74\", pch=16, col=\"blue\")\n\n# \u793a\u4f8b2: \u7ed8\u5236\u6298\u7ebf\u56fe\nx &lt;- seq(0, 2*pi, length.out=100)\ny &lt;- sin(x)\nplot(x, y, type=\"l\", main=\"\u6b63\u5f26\u66f2\u7ebf\", xlab=\"\u89d2\u5ea6(\u5f27\u5ea6)\", ylab=\"\u6b63\u5f26\u503c\", col=\"red\")\n\n# \u793a\u4f8b3: \u6dfb\u52a0\u70b9\u548c\u7ebf\u5230\u73b0\u6709\u56fe\u8868\npoints(x&#91;1:20], y&#91;1:20], pch=19, col=\"green\")  # \u6dfb\u52a0\u7eff\u8272\u70b9\nlines(x&#91;21:40], y&#91;21:40], lwd=2, col=\"purple\")  # \u6dfb\u52a0\u7d2b\u8272\u7ebf\n\n# \u793a\u4f8b4: \u7ed8\u5236\u7bb1\u7ebf\u56fe\ndata &lt;- list(\u7ec4A=rnorm(50, 5, 1), \u7ec4B=rnorm(50, 7, 1.5))\nboxplot(data, main=\"\u7bb1\u7ebf\u56fe\u6bd4\u8f83\", ylab=\"\u6570\u503c\")\n```\n## 2. ecdf()\u7d2f\u79ef\u5206\u5e03\u51fd\u6570\u793a\u4f8b\n\n```{r}\n# \u751f\u6210\u968f\u673a\u6570\u636e\nset.seed(123)  # \u8bbe\u7f6e\u968f\u673a\u79cd\u5b50\u786e\u4fdd\u7ed3\u679c\u53ef\u91cd\u73b0\ndata &lt;- rnorm(1000, mean=0, sd=1)  # \u751f\u62101000\u4e2a\u6b63\u6001\u5206\u5e03\u968f\u673a\u6570\n\n# \u8ba1\u7b97\u7ecf\u9a8c\u7d2f\u79ef\u5206\u5e03\u51fd\u6570(ECDF)\nempirical_cdf &lt;- ecdf(data)\n\n# \u67e5\u770b\u7279\u5b9a\u503c\u7684\u7d2f\u79ef\u6982\u7387\nprint(paste(\"x &lt;= 0\u7684\u6982\u7387:\", empirical_cdf(0)))  # \u7ea6\u4e3a0.5\nprint(paste(\"x &lt;= 1.96\u7684\u6982\u7387:\", empirical_cdf(1.96)))  # \u7ea6\u4e3a0.975\n\n# \u7ed8\u5236ECDF\u56fe\nplot(empirical_cdf, \n     main=\"\u6b63\u6001\u5206\u5e03\u7684\u7ecf\u9a8c\u7d2f\u79ef\u5206\u5e03\u51fd\u6570\", \n     xlab=\"x\u503c\", \n     ylab=\"\u7d2f\u79ef\u6982\u7387\", \n     col=\"blue\", \n     lwd=2)\n\n# \u6dfb\u52a0\u7406\u8bbaCDF\u66f2\u7ebf\u4f5c\u4e3a\u5bf9\u6bd4\ncurve(pnorm(x, mean=0, sd=1), \n      add=TRUE, \n      col=\"red\", \n      lwd=2, \n      lty=2)  # \u6dfb\u52a0\u6807\u51c6\u6b63\u6001\u5206\u5e03\u7684\u7406\u8bbaCDF\u66f2\u7ebf\n\n# \u6dfb\u52a0\u56fe\u4f8b\nlegend(\"topleft\", \n       legend=c(\"\u7ecf\u9a8cCDF\", \"\u7406\u8bbaCDF\"), \n       col=c(\"blue\", \"red\"), \n       lwd=2, \n       lty=c(1, 2))\n```\n## 3. \u81ea\u5b9a\u4e49 ECDF \u56fe\u793a\u4f8b\n```{r}\n# \u4f7f\u7528ggplot2\u7ed8\u5236\u66f4\u7f8e\u89c2\u7684ECDF\u56fe\nlibrary(ggplot2)\n\n# \u751f\u6210\u4e24\u7ec4\u6570\u636e\u8fdb\u884c\u6bd4\u8f83\ngroup1 &lt;- rnorm(100, mean=5, sd=1)\ngroup2 &lt;- rnorm(100, mean=7, sd=1.5)\n\n# \u521b\u5efa\u6570\u636e\u6846\ndf &lt;- data.frame(\n  value = c(group1, group2),\n  group = rep(c(\"\u7ec4A\", \"\u7ec4B\"), each = 100)\n)\n\n# \u4f7f\u7528ggplot2\u7ed8\u5236ECDF\u56fe\nggplot(df, aes(x = value, color = group)) +\n  stat_ecdf(size = 1) +\n  labs(\n    title = \"\u4e24\u7ec4\u6570\u636e\u7684\u7d2f\u79ef\u5206\u5e03\u51fd\u6570\u6bd4\u8f83\",\n    x = \"\u6570\u503c\",\n    y = \"\u7d2f\u79ef\u6982\u7387\",\n    color = \"\u7ec4\u522b\"\n  ) +\n  theme_minimal() +\n  theme(\n    plot.title = element_text(hjust = 0.5, size = 16, face = \"bold\"),\n    axis.title = element_text(size = 12),\n    legend.position = \"bottom\"\n  ) +\n  scale_color_manual(values = c(\"\u7ec4A\" = \"#3366CC\", \"\u7ec4B\" = \"#CC6677\"))\n```<\/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-5617","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\/5617","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=5617"}],"version-history":[{"count":1,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/5617\/revisions"}],"predecessor-version":[{"id":5618,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/5617\/revisions\/5618"}],"wp:attachment":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5617"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}