rdataframeggplot2likert

gglikert in R with grouping factors


i have a data frame in R that looks like this :

 df
# A tibble: 150 × 7
   grouping q1                         q2                         q3                         q4                         q5    q6   
   <chr>    <fct>                      <fct>                      <fct>                      <fct>                      <fct> <fct>
 1 A        Disagree                   Strongly disagree          Strongly agree             Agree                      Agree Stro…
 2 A        Strongly disagree          Agree                      Neither agree nor disagree Neither agree nor disagree Agree Stro…
 3 A        Neither agree nor disagree Disagree                   Strongly disagree          Strongly agree             Neit… Neit…
 4 A        Strongly disagree          Neither agree nor disagree Agree                      Disagree                   Stro… Neit…
 5 B        Strongly agree             Strongly disagree          Strongly agree             Disagree                   NA    Agree
 6 D        Strongly disagree          Neither agree nor disagree Agree                      Strongly agree             Neit… Neit…
 7 B        Neither agree nor disagree Disagree                   Neither agree nor disagree Neither agree nor disagree Stro… Neit…
 8 B        Strongly disagree          Strongly agree             Neither agree nor disagree Agree                      Neit… Stro…
 9 A        Neither agree nor disagree Agree                      Strongly agree             Strongly disagree          Neit… Agree
10 D        Strongly agree             Disagree                   Agree                      Disagree                   Agree Neit…
# ℹ 140 more rows
# ℹ Use `print(n = ...)` to see more rows

i want to use gglikert from ggstats package to plot it but in the y axis to have the grouping factors / categories . How can i do it in R using gglikert() or ggplot2?

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

Solution

  • I think you want the facet_rows argument.

    gglikert(df, q1:q6, facet_rows = vars(grouping))
    

    enter image description here