rggplot2facetfacet-wrap

Change position of facet label fro right to top in ggplot2 in R


I want to change the facet label position from right to top. How can I do it in ggplot2 under the gglikert() function ?

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))

enter image description here


Solution

  • ggstats::gglikert uses facet_grid under the hood but you can achieve your desired result by overwriting with facet_wrap:

    library(ggstats)
    library(ggplot2)
    
    ggstats::gglikert(df, q1:q6, facet_rows = vars(grouping)) +
      facet_wrap(~grouping, ncol = 1)