Search code examples
rggplot2likert

long group with many categories in gglikert in R


i have a data frame with 3 columns of likert level values and a column grouping with many categories. For presenttation purposes i made letters level from A to Z (but ignore it please because the real life data set has other more complicated character values).

The question is : How can i pass all this categories in "?" space code in order to plot this likert chart ?



library(dplyr)
library(tidyr)
library(ggstats)


set.seed(123)
likert_levels <- c(
  "1" = "Very  Dissatisfied",
  "2" = "Dissatisfied",
  "3" = "Neutral",
  "4" = "Satisfied",
  "5" = "Very  Satisfied")
df = data.frame(
  var = sample(LETTERS, 50, replace = TRUE),
  A = sample(likert_levels, 50, replace = TRUE),
  B = sample(likert_levels, 50, replace = TRUE),
  C = sample(likert_levels, 50, replace = TRUE))%>%
  mutate(across(everything(),as.factor))%>%
  as_tibble()%>%
  mutate(across(-var, ~ factor(.x, levels = likert_levels)))
mutate(df, id=row_number()) %>%
  pivot_longer(-c(id, var), names_to="group") %>%
  pivot_wider(names_from=var)%>%
  ggstats::gglikert( "?", 
           facet_rows=vars(group))



Solution

  • Similar to tidyverse verbs gglikert allows to use tidy-select syntax to select the columns to include=, i.e. instead of providing the columns to include you can provide the columns to exclude using - or !. And referring to your comment, this also works when there is only one var column:

    library(dplyr, warn = FALSE)
    library(tidyr)
    library(ggstats)
    
    mutate(df, id = row_number()) %>%
      pivot_longer(-c(id, var), names_to = "group") %>%
      pivot_wider(names_from = var) %>%
      ggstats::gglikert(-c(id, group), facet_rows = vars(group))