{"id":6432,"date":"2025-10-12T00:23:14","date_gmt":"2025-10-11T16:23:14","guid":{"rendered":"http:\/\/cnliutz.ipyingshe.net\/?p=6432"},"modified":"2025-10-12T00:23:16","modified_gmt":"2025-10-11T16:23:16","slug":"r%e8%af%ad%e8%a8%80%e6%96%b9%e5%b7%ae%e5%88%86%e6%9e%90%e4%bd%9c%e5%9b%be","status":"publish","type":"post","link":"http:\/\/xc.ipyingshe.net:5347\/?p=6432","title":{"rendered":"R\u8bed\u8a00\u65b9\u5dee\u5206\u6790\u4f5c\u56fe"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code># \u52a0\u8f7d\u5fc5\u8981\u7684\u5305\nlibrary(dplyr)\nlibrary(ggplot2)\nlibrary(car)\nlibrary(effects)\nlibrary(multcomp)\nlibrary(gridExtra)\n\n# \u8bbe\u7f6e\u968f\u673a\u79cd\u5b50\u4ee5\u786e\u4fdd\u7ed3\u679c\u53ef\u91cd\u73b0\nset.seed(123)\n\n# ----------------------------\n# \u5355\u56e0\u7d20\u65b9\u5dee\u5206\u6790\u7ed8\u56fe\u793a\u4f8b\n# ----------------------------\n\n# \u4f7f\u7528\u5185\u7f6e\u7684PlantGrowth\u6570\u636e\u96c6\ndata(PlantGrowth)\n\n# 1. \u7bb1\u7ebf\u56fe\uff1a\u5c55\u793a\u5404\u7ec4\u5206\u5e03\np1 &lt;- ggplot(PlantGrowth, aes(x = group, y = weight)) +\n  geom_boxplot(fill = \"lightblue\", color = \"black\") +\n  theme_minimal() +\n  labs(\n    title = \"\u7bb1\u7ebf\u56fe\uff1a\u4e0d\u540c\u5904\u7406\u7ec4\u7684\u690d\u7269\u91cd\u91cf\",\n    x = \"\u5904\u7406\u7ec4\",\n    y = \"\u91cd\u91cf\"\n  )\n\n# 2. \u5747\u503c\u56fe + \u7f6e\u4fe1\u533a\u95f4\nsummary_data &lt;- PlantGrowth %>%\n  group_by(group) %>%\n  summarize(\n    mean = mean(weight),\n    sd = sd(weight),\n    n = n(),\n    se = sd \/ sqrt(n),\n    lower = mean - qt(0.975, df = n - 1) * se,\n    upper = mean + qt(0.975, df = n - 1) * se\n  )\n\np2 &lt;- ggplot(summary_data, aes(x = group, y = mean, group = 1)) +\n  geom_point(size = 3, color = \"red\") +\n  geom_line(linetype = \"dashed\", color = \"darkgrey\", linewidth = 0.7) +  # \u5c06size\u6539\u4e3alinewidth\n  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.1, linewidth = 0.8) +  # \u5c06size\u6539\u4e3alinewidth\n  theme_minimal() +\n  labs(\n    title = \"\u5747\u503c\u56fe + 95%\u7f6e\u4fe1\u533a\u95f4\",\n    x = \"\u5904\u7406\u7ec4\",\n    y = \"\u5e73\u5747\u91cd\u91cf\"\n  )\n\n# 3. \u6563\u70b9\u56fe + \u6296\u52a8\np3 &lt;- ggplot(PlantGrowth, aes(x = group, y = weight)) +\n  geom_jitter(width = 0.2, alpha = 0.6, color = \"blue\") +\n  stat_summary(fun = \"mean\", geom = \"point\", color = \"red\", size = 4) +\n  theme_minimal() +\n  labs(\n    title = \"\u6563\u70b9\u56fe + \u5747\u503c\u70b9\",\n    x = \"\u5904\u7406\u7ec4\",\n    y = \"\u91cd\u91cf\"\n  )\n\n# ----------------------------\n# \u53cc\u56e0\u7d20\u65b9\u5dee\u5206\u6790\u7ed8\u56fe\u793a\u4f8b\n# ----------------------------\n\n# \u521b\u5efa\u793a\u4f8b\u6570\u636e\u96c6\nn_per_group &lt;- 10  # \u6bcf\u7ec4\u6837\u672c\u91cf\n\n# \u521b\u5efa\u56e0\u5b50\u7ec4\u5408\nmydata &lt;- expand.grid(\n  gender = factor(c(\"Male\", \"Female\")),\n  treatment = factor(c(\"Control\", \"Low\", \"High\"))\n)\n\n# \u4e3a\u6bcf\u7ec4\u5b9a\u4e49\u5747\u503c\uff08\u5305\u542b\u4e3b\u6548\u5e94\u548c\u4ea4\u4e92\u6548\u5e94\uff09\nmeans_matrix &lt;- matrix(\n  c(50, 60, 75,  # \u7537\u6027\u5728\u5404\u5904\u7406\u6c34\u5e73\u4e0b\u7684\u5747\u503c\n    55, 75, 70), # \u5973\u6027\u5728\u5404\u5904\u7406\u6c34\u5e73\u4e0b\u7684\u5747\u503c\n  nrow = 2, byrow = TRUE,\n  dimnames = list(c(\"Male\", \"Female\"), c(\"Control\", \"Low\", \"High\"))\n)\n\n# \u751f\u6210\u6570\u636e\nmydata$mean &lt;- as.vector(means_matrix)\nmydata &lt;- mydata&#91;rep(1:nrow(mydata), each = n_per_group), ]\nmydata$score &lt;- mydata$mean + rnorm(nrow(mydata), 0, 8)\n\n# 4. \u4ea4\u4e92\u6548\u5e94\u56fe\nmodel &lt;- aov(score ~ gender * treatment, data = mydata)\neffect_data &lt;- as.data.frame(effect(\"gender:treatment\", model))\n\np4 &lt;- ggplot(effect_data, aes(x = treatment, y = fit, group = gender, color = gender)) +\n  geom_line(linewidth = 1.2) +  # \u5c06size\u6539\u4e3alinewidth\n  geom_point(size = 3) +\n  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.1, linewidth = 0.8) +  # \u5c06size\u6539\u4e3alinewidth\n  theme_minimal() +\n  labs(\n    title = \"\u6027\u522b \u00d7 \u5904\u7406 \u4ea4\u4e92\u6548\u5e94\u56fe\",\n    x = \"\u5904\u7406\u6c34\u5e73\",\n    y = \"\u9884\u6d4b\u5206\u6570\",\n    color = \"\u6027\u522b\"\n  ) +\n  scale_color_manual(values = c(\"Male\" = \"blue\", \"Female\" = \"red\"))\n\n# 5. \u7bb1\u7ebf\u56fe\uff1a\u53cc\u56e0\u7d20\u8bbe\u8ba1\np5 &lt;- ggplot(mydata, aes(x = treatment, y = score, fill = gender)) +\n  geom_boxplot(position = position_dodge(0.8)) +\n  theme_minimal() +\n  labs(\n    title = \"\u53cc\u56e0\u7d20\u7bb1\u7ebf\u56fe\",\n    x = \"\u5904\u7406\u6c34\u5e73\",\n    y = \"\u5206\u6570\",\n    fill = \"\u6027\u522b\"\n  )\n\n# 6. \u6563\u70b9\u56fe + \u5206\u7ec4\np6 &lt;- ggplot(mydata, aes(x = treatment, y = score, color = gender)) +\n  geom_jitter(alpha = 0.6, position = position_jitterdodge(jitter.width = 0.2, dodge.width = 0.8)) +\n  stat_summary(fun = \"mean\", geom = \"point\", size = 4, position = position_dodge(0.8)) +\n  theme_minimal() +\n  labs(\n    title = \"\u53cc\u56e0\u7d20\u6563\u70b9\u56fe\",\n    x = \"\u5904\u7406\u6c34\u5e73\",\n    y = \"\u5206\u6570\",\n    color = \"\u6027\u522b\"\n  )\n\n# 7. \u6b8b\u5dee\u8bca\u65ad\u56fe\nfit &lt;- aov(weight ~ group, data = PlantGrowth)\npar(mfrow = c(2, 2))\nplot(fit)\npar(mfrow = c(1, 1))\n\n# \u663e\u793a\u6240\u6709\u56fe\u5f62\ngrid.arrange(p1, p2, p3, p4, p5, p6, ncol = 2)    \n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"927\" height=\"1024\" src=\"http:\/\/cnliutz.ipyingshe.net\/wp-content\/uploads\/2025\/10\/image-3-927x1024.png\" alt=\"\" class=\"wp-image-6433\" srcset=\"http:\/\/xc.ipyingshe.net:5347\/wp-content\/uploads\/2025\/10\/image-3-927x1024.png 927w, http:\/\/xc.ipyingshe.net:5347\/wp-content\/uploads\/2025\/10\/image-3-272x300.png 272w, http:\/\/xc.ipyingshe.net:5347\/wp-content\/uploads\/2025\/10\/image-3-768x849.png 768w, http:\/\/xc.ipyingshe.net:5347\/wp-content\/uploads\/2025\/10\/image-3.png 1192w\" sizes=\"auto, (max-width: 927px) 100vw, 927px\" \/><\/figure>\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-6432","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/6432","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6432"}],"version-history":[{"count":1,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/6432\/revisions"}],"predecessor-version":[{"id":6434,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/6432\/revisions\/6434"}],"wp:attachment":[{"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6432"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}