You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R的ggplot2中通过gglikert()将分面标签从右侧移至顶部

如何在ggplot2框架下用gglikert()将分面标签从右侧移至顶部

我需要把分面标签的位置从右侧调整到顶部,请问怎么在R语言的ggplot2中使用gglikert()函数实现?以下是我的示例代码和生成的可视化效果:

library(tidyverse)
library(ggstats)


likert_levels <- c(
  "Strongly disagree",
  "Disagree",
  "Neither agree nor disagree",
  "Agree",
  "Strongly agree"
)

df <-
  tibble(
    grouping = sample(c("A", "B", "C", "D"), 150, replace = TRUE),
    q1 = sample(likert_levels, 150, replace = TRUE),
    q2 = sample(likert_levels, 150, replace = TRUE, prob = 5:1),
    q3 = sample(likert_levels, 150, replace = TRUE, prob = 1:5),
    q4 = sample(likert_levels, 150, replace = TRUE, prob = 1:5),
    q5 = sample(c(likert_levels, NA), 150, replace = TRUE),
    q6 = sample(likert_levels, 150, replace = TRUE, prob = c(1, 0, 1, 1, 0))
  ) %>%
  mutate(across(-grouping, ~ factor(.x, levels = likert_levels)))
df
ggstats::gglikert(df, q1:q6, facet_rows = vars(grouping))

原可视化效果


解决方案

方法1:切换为横向分面(标签默认在顶部)

将原代码中的facet_rows参数替换为facet_cols,分面会按横向排列,标签自动显示在每个分面的顶部:

ggstats::gglikert(df, q1:q6, facet_cols = vars(grouping))

方法2:保留纵向分面,调整标签位置

如果需要保持分面的纵向上下排列,可通过ggplot2的theme()函数修改strip.position参数,把右侧标签移到顶部:

ggstats::gglikert(df, q1:q6, facet_rows = vars(grouping)) +
  theme(strip.position = "top")

说明:此方法下分面仍为纵向布局,但分面标签会统一显示在图表顶部区域。


内容的提问来源于stack exchange,提问作者Homer Jay Simpson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 12:46:15