公開・実測日:2026 年 11 月 18 日。
修正後の図はどう変わりますか?
灰色は No answer(無回答)で、Access、Cost、Timing はそれぞれ 4%、6%、2% です。前後の図は同じ架空の集計値を使用し、各設問の回答数は無回答を含めて 100 件です。変えたのは配置と表示です。本記事では説明用のシミュレーションデータを使います。
スマートフォンでは図を横にスクロールするか、原寸画像を開いてください。


この図を再現する R スクリプトはどれですか?
必要に応じて CRAN から ggplot2、ggstats、patchwork、labelled をインストールしてください。labelled は推奨パッケージですが、この呼び出しでは必要でした。書き込み可能な作業ディレクトリで実行すると 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()再現用ファイル: R スクリプト · 修正後 PNG · 修正後 PDF · 架空の集計値 CSV.
横軸の範囲とパネル幅を別々に変えるのはなぜですか?
検証した gglikert_side() は、両サブプロットに固定座標を使った patchwork を返します。サイドの割合が小さいと、その横軸範囲も小さくなります。gglikert_side() の width は棒の太さを変えます。私はまず固定比率を外し、サイド軸を設定してから描画領域の幅を 4:1 にします。この比率は描画領域の幅に適用されます。
ggstats 0.14.0 では 2 番目がサイドプロットです。更新後はオブジェクトを確認してください。20% の上限はこの例用です。scale の limits は範囲外のデータを除くため、自分の最大値を確認します。サイドに複数カテゴリを置く場合は合計割合を確認してください。本例の stopifnot は単一カテゴリの割合が設定範囲に収まらなければ停止します。
割合と書き出し結果は何を確認しましたか?
無回答は Access が 4/100、Cost が 6/100、Timing が 2/100 です。他の 5 カテゴリと無回答の合計は設問ごとに 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 です。パッケージのソースや掲示板の画像、回答者データは配布しません。
通常の 2 図の書き出しは ggplot2 書き出しガイド、ピクセル換算は画像サイズ計算ツールをご覧ください。
R言語入門

