发布与实测日期:2026年11月19日。
另建一张每组一行的汇总表,分别绘制均值标记和误差线,两层都设置 inherit.aes = FALSE,使用各自的映射,避免将每个原始观测当成一条汇总记录。误差线官方文档说明了区间端点的指定方式。
加上汇总层后,图会变成什么样?
我会先看散点。每组就五个数,可以对着它们找到均值标记,再看上下各一个样本标准差有多长。


这个例子的均值和误差线端点是多少?
A组为8、9、10、11、12,B组为12、13、14、15、16,每组5个观测。均值分别为10和14。每组相对组均值的离差平方和均为10,样本SD为 sqrt(10 / (5 - 1)),约1.581139。A组误差线约为8.418861至11.581139,B组约为12.418861至15.581139。R的sd文档明确采用n − 1作分母。
怎样保持原始值,并让误差线对准组别?
原始表和汇总表使用相同的组别因子水平。汇总层都将组别映射到x轴,均值和误差线保留在类别中心。原始点用 position_jitter(height = 0, seed = 11),纵坐标不抖动,横向排列也能重复。抖动参数文档。
这里的误差线用样本 SD 描述离散程度,图注需写明这一定义。需要检验均值差异时,另行选择合适的分析方法。代码假定观测为有限数值、每组至少两个;换用其他数据前先检查缺失值与分组。
怎样复现并下载完整结果?
准备好ggplot2,在空的输出目录运行 Rscript --vanilla example.R。已在R 4.5.3与ggplot2 4.0.4运行。PNG实测1200 × 900像素、约300 PPI,PDF页面为101.6 × 76.2 mm。尺寸仅供示例,未核对PDF字体嵌入是否符合投稿要求。
下面是本文用到的代码、数据和结果文件。你可以下载代码跟着操作,也可以打开结果文件,对照自己运行后的结果。
- example.R可以在电脑上运行的完整示例代码。
- synthetic.csv本例使用的输入数据,可作为表格打开。
- summary.csv示例计算得到的数值表格。
- before.png修改前的图,用来比较。
- after.png按本文设置生成的结果图。
- after.pdfPDF 格式的结果图,方便查看和排版。
回归置信带配色见独立的geom_smooth解答,尺寸换算用像素计算器。
# Original synthetic values, code and figures: CC0-1.0.
library(ggplot2)
d <- data.frame(group = factor(rep(c("A", "B"), each = 5), levels = c("A", "B")),
value = c(8, 9, 10, 11, 12, 12, 13, 14, 15, 16))
parts <- split(d$value, d$group)
summary <- data.frame(group = factor(names(parts), levels = levels(d$group)),
n = vapply(parts, length, integer(1)),
mean = vapply(parts, mean, numeric(1)),
sd = vapply(parts, sd, numeric(1)))
summary$lower <- summary$mean - summary$sd
summary$upper <- summary$mean + summary$sd
# Independent hand-check: both groups have sum of squared deviations 10.
stopifnot(all(summary$n == 5), identical(unname(summary$mean), c(10, 14)),
max(abs(summary$sd - sqrt(10/4))) < 1e-12,
all(vapply(parts, function(x) sum((x - mean(x))^2), numeric(1)) == 10))
before <- ggplot(d, aes(group, value)) +
geom_point(position = position_jitter(width = .06, height = 0, seed = 11),
size = 2, colour = "#17665c") +
scale_x_discrete(labels = c(A = "A (n = 5)", B = "B (n = 5)")) +
scale_y_continuous(limits = c(7, 17), breaks = seq(8, 16, 2)) +
labs(x = "Group", y = "Response (arbitrary units)",
caption = "Synthetic data; small dots are observations") +
theme_classic(base_size = 10) +
theme(plot.caption = element_text(size = 7), plot.margin = margin(7, 8, 7, 7))
after <- before +
geom_errorbar(data = summary, aes(x = group, ymin = lower, ymax = upper),
inherit.aes = FALSE, width = .16, linewidth = .6) +
geom_point(data = summary, aes(x = group, y = mean), inherit.aes = FALSE,
shape = 23, size = 3.5, fill = "#D55E00", colour = "black") +
labs(caption = "Synthetic data; diamonds: means; error bars: +/-1 sample SD")
b <- ggplot_build(before)
a <- ggplot_build(after)
stopifnot(nrow(a$data[[1]]) == 10L,
identical(a$data[[1]][c("x", "y")], b$data[[1]][c("x", "y")]),
all(a$data[[1]]$y == d$value),
max(abs(a$data[[2]]$ymin - summary$lower)) < 1e-12,
max(abs(a$data[[2]]$ymax - summary$upper)) < 1e-12,
all(as.numeric(a$data[[2]]$x) == as.numeric(a$data[[3]]$x)))
ggsave("before.png", before, width = 1200, height = 900, units = "px", dpi = 300, bg = "white")
ggsave("after.png", after, width = 1200, height = 900, units = "px", dpi = 300, bg = "white")
ggsave("after.pdf", after, width = 101.6, height = 76.2, units = "mm", bg = "white")
write.csv(d, "synthetic.csv", row.names = FALSE)
write.csv(summary, "summary.csv", row.names = FALSE)
capture.output(sessionInfo(), file = "session-info.txt")
print(summary)
cat("PASS: 10 observations; n=5 in each group; means10/14; sampleSD=sqrt(2.5); y values and seeded x jitter unchanged; interval endpoints and group centres match.\n")
问题来源与示例许可是什么?
选题来自2026年10月3日的 Reddit 问题。代码和图形为本文自行编写与绘制。
原创示例脚本、模拟观测、汇总表及 PNG/PDF 以 CC0-1.0 提供,允许商业复用。R 保留 GPL-2 或 GPL-3,ggplot2 保留 MIT;下载不包含包的软件代码。
从零开始学习R语言

