将Likert图分区标签移至顶部而非左侧时图表消失的求助
问题
我想要创建一个基于type列两个因子分组的问题图表,每个问题对应一个分区。当前代码生成的图表已经接近需求,但分区标签显示在左侧而非顶部。将strip.left改为FALSE、strip设置为strip.custom(bg = "gray97")后,标签移到了顶部,但图表内容消失了。调整between和resize.height.tuning参数仅增加了标签间的空间,图表依然不显示。请问如何将分区标题移至每个图表顶部,同时保留图表的显示空间?
当前使用的代码:
likert(type ~ . | Item, data = first_questions_data, layout = c(1,20), scales = list(y = list(relation = "free")), between = list(y = 1), strip.left = strip.custom(bg = "gray97"), strip = FALSE, par.strip.text = list(cex = 1.1, lines = 2), ylab = NULL, cex = 1.2, ReferenceZero = 3, as.percent = TRUE, positive.order = TRUE, main = "As a result of the Dobbs decision I spend more time...", xlim = c(-100, -80, -60, -40, -20, 0, 20, 40, 60, 80, 100), resize.height.tuning = 1)
调整后的代码:
likert(type ~ . | Item, data = first_questions_data, layout = c(1,20), scales = list(y = list(relation = "free")), between = list(y = 1), strip.left = FALSE, strip = strip.custom(bg = "gray97"), par.strip.text = list(cex = 1.1, lines = 2), ylab = NULL, cex = 1.2, ReferenceZero = 3, as.percent = TRUE, positive.order = TRUE, main = "As a result of the Dobbs decision I spend more time...", xlim = c(-100, -80, -60, -40, -20, 0, 20, 40, 60, 80, 100), resize.height.tuning = 1)
解决方案
问题核心在于布局设置和likert函数的空间分配逻辑:
- 你设置了
layout = c(1,20),把20个图表强行排成1行20列,横向空间被顶部标签和布局挤压到极致,直接导致图表内容被压缩得完全看不见。 resize.height.tuning仅作用于纵向高度调整,对横向布局的空间问题完全无效。
修正方案
方案1:改为纵向布局(最稳妥)
把图表改成从上到下的纵向排列,给顶部标签和图表内容都预留足够空间:
likert(type ~ . | Item, data = first_questions_data, layout = c(20,1), scales = list(y = list(relation = "free")), between = list(y = 0.8), strip.left = FALSE, strip = strip.custom(bg = "gray97"), par.strip.text = list(cex = 1.1, lines = 1.5), ylab = NULL, cex = 1.2, ReferenceZero = 3, as.percent = TRUE, positive.order = TRUE, main = "As a result of the Dobbs decision I spend more time...", xlim = c(-100, -80, -60, -40, -20, 0, 20, 40, 60, 80, 100), resize.height.tuning = 0.9)
方案2:保留横向布局(需调整窗口宽度)
如果一定要横向排列,需要减少每行的图表数量,同时手动打开足够宽的绘图窗口:
# 先打开宽幅绘图设备(以PDF为例) pdf("likert_plot.pdf", width = 40, height = 12) likert(type ~ . | Item, data = first_questions_data, layout = c(4,5), scales = list(y = list(relation = "free")), between = list(x = 0.5, y = 0.5), strip.left = FALSE, strip = strip.custom(bg = "gray97"), par.strip.text = list(cex = 1.1, lines = 1.5), ylab = NULL, cex = 1.2, ReferenceZero = 3, as.percent = TRUE, positive.order = TRUE, main = "As a result of the Dobbs decision I spend more time...", xlim = c(-100, -80, -60, -40, -20, 0, 20, 40, 60, 80, 100)) dev.off()
内容的提问来源于stack exchange,提问作者David Beales
相关产品推荐
相关产品推荐

