发布与实测日期:2026 年 11 月 18 日。
修改后的实际图是什么样?
灰色侧栏表示 No answer(未回答),Access、Cost、Timing 三项分别为 4%、6%、2%。前后两图使用同一组自制计数,每题 100 份回答,分母包含未回答者。修改的是布局与标注。本文使用模拟演示数据
手机上可在图内横向滚动,或打开原尺寸图片查看。


哪段完整 R 代码可以生成这些图?
需要时先从 CRAN 安装 ggplot2、ggstats、patchwork、labelled。此次调用确实需要 labelled,虽然它列在 ggstats 的建议依赖中。在可写工作目录运行脚本,会生成 likert-output 文件夹。实测版本为 R 4.5.3、ggstats 0.14.0、ggplot2 4.0.4、patchwork 1.3.2、labelled 2.16.1、scales 1.4.0。
install.packages(c("ggplot2", "ggstats", "patchwork", "labelled"))# Bopbun original example. Code: CC0-1.0.
# Tested: R 4.5.3, ggstats 0.14.0, ggplot2 4.0.4, patchwork 1.3.2.
library(ggplot2)
library(ggstats)
library(patchwork)
answers <- c("Strongly disagree", "Disagree", "Neutral",
"Agree", "Strongly agree", "No answer")
counts <- list(
Access = c(10, 15, 20, 25, 26, 4),
Cost = c(8, 15, 24, 30, 17, 6),
Timing = c(20, 18, 20, 20, 20, 2)
)
survey <- as.data.frame(lapply(counts, function(n) {
factor(rep(answers, times = n), levels = answers)
}))
# Every question has 100 responses, including the side category.
stopifnot(all(vapply(counts, sum, numeric(1)) == 100))
side_share <- vapply(survey, function(x) mean(x == "No answer"), numeric(1))
stopifnot(max(side_share) < 0.2)
p <- gglikert_side(
survey, side_values = "No answer", add_totals = FALSE,
labels_hide_below = 0.07, labels_size = 3, labels_accuracy = 1
) & theme_minimal(base_size = 10)
p[[1]] <- p[[1]] + theme(legend.position = "bottom") +
guides(fill = guide_legend(nrow = 2))
p[[2]] <- p[[2]] + labs(title = "No answer") + theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank(), legend.position = "none")
before <- p + plot_annotation(
title = "Synthetic survey: default side axis",
caption = "100 responses per question; No answer is included in the denominator."
)
# In ggstats 0.14.0, component 2 is the side plot.
# Give the 2-6% side values room, and use just three tick positions.
fixed <- p
fixed[[1]] <- fixed[[1]] + coord_cartesian()
fixed[[2]] <- fixed[[2]] + scale_x_continuous(
limits = c(0, 0.2), breaks = c(0, 0.1, 0.2),
labels = scales::label_percent(accuracy = 1),
expand = expansion(mult = c(0, 0.05))
) + coord_cartesian() + geom_text(
data = data.frame(question = names(side_share), share = unname(side_share)),
aes(x = share, y = question, label = scales::percent(share, accuracy = 1)),
inherit.aes = FALSE, hjust = -0.3, size = 3, colour = "black"
)
fixed <- fixed + plot_layout(widths = c(4, 1))
after <- fixed + plot_annotation(
title = "Synthetic survey: explicit side axis and panel widths",
caption = "100 responses per question; No answer is included in the denominator."
)
dir.create("likert-output", showWarnings = FALSE)
for (name in c("before", "after")) {
ggsave(file.path("likert-output", paste0(name, ".png")),
plot = get(name), width = 180, height = 100, units = "mm",
dpi = 300, device = "png", bg = "white")
}
ggsave("likert-output/after.pdf", plot = after,
width = 180, height = 100, units = "mm", device = "pdf")
write.csv(data.frame(Response = answers, counts),
"likert-output/counts.csv", row.names = FALSE)
print(side_share)
sessionInfo()为什么要分别改横轴范围和绘图区宽度?
实测版本的 gglikert_side() 返回 patchwork 组合图,两个子图使用固定纵横比。侧栏比例小时,右图横轴范围也小。在 gglikert_side() 里改 width 会改变柱条厚度。我先解除固定纵横比,设好侧栏刻度,再按 4:1 分配绘图区宽度;这个比例不包含所有标签及边距。
在 ggstats 0.14.0 中,第 2 个子图是侧栏;升级包后应检查对象。0–20% 只适合此示例。设置上限前要核对侧栏最大比例,坐标尺度 limits 会移除范围外数据。若侧栏有多个类别,要检查它们的合计比例。代码中的 stopifnot 会在本例单个类别超过预设范围时主动停止。
哪些比例和导出属性已经实际核对?
Access 的未回答为 4/100,Cost 为 6/100,Timing 为 2/100。每题其余五类计数与未回答合计 100。把未回答移到侧栏并不会把它移出分母;只有确实要改变分母时,才考虑把它重编码为 NA。
| 模拟题目 | 未回答 | 总回答 | 侧栏比例 |
|---|---|---|---|
| Access | 4 | 100 | 4% |
| Cost | 6 | 100 | 6% |
| Timing | 2 | 100 | 2% |
两张 PNG 都按 180 × 100 mm、300 DPI 导出,实际为 2,125 × 1,181 像素,存在整像素换算;文件记录的分辨率约为 300 PPI。PDF 请求尺寸为 180 × 100 mm,实际 MediaBox 为 510 × 283 点,即 179.92 × 99.84 mm。单纯提高 DPI 不会重新安排标签,也不会扩大侧栏。
可以在哪里核对行为和复用示例?
选题来自 2026 年 11 月 17 日一条公开 Reddit 问题。原作者没有提供数据,因此本文独立复现相同类型的布局问题,不声称修好了原作者的数据。包行为同时对照了官方文档与本机安装的函数。
- ggstats: gglikert_side()
- patchwork: plot_layout()
- ggplot2: coord_cartesian()
- ggplot2: scale_x_continuous()
- ggplot2: ggsave()
- Reddit: 2026-11-17
本文示例代码、自制计数及生成的 PNG/PDF 以 CC0-1.0 提供,可用于商业用途。R 包保留各自许可:ggstats 与 labelled 为 GPL ≥ 3,ggplot2 与 patchwork 为 MIT。下载文件不包含包源码、论坛图片或受访者数据。
普通双图导出见 ggplot2 导出教程;像素换算可使用图片尺寸计算器。
从零开始学习R语言

