Search code examples
rggplot2likert

Specify the height of the bars in the gglikert function in R


I am creating g a Rmarkdown document that renders to pdf.

Inside this Rmarkdown document I have two chunks both of them simulated data frame and gglikert from here.

The first chunk has a plot with 6 likert questions and the second on e with 3.

Without specifying the width and the height of the chunk in the chunk options.

I understand that the height of the bar is proportional to number of the questions.

However trying to specify in both plots a specific number of heigh I ended up here. Trying to implementing the solution in my problem it seems that has no effect on the height of the bars.

Can anyone has another idea about it ?

---
title: "Untitled"
output: pdf_document
date: "2025-03-01"
---
knitr::opts_chunk$set(echo = TRUE)
parameter = 0.1
library(ggstats)
library(dplyr)
library(ggplot2)


likert_levels <- c(
  "Strongly disagree",
  "Disagree",
  "Neither agree nor disagree",
  "Agree",
  "Strongly agree"
)
set.seed(42)
df <-
  tibble(
    grouping = sample(c(LETTERS[1:9]), 150, replace = TRUE),
    q1 = sample(c(likert_levels, NA), 150, replace = TRUE),
    q2 = sample(c(likert_levels, NA), 150, replace = TRUE),
    q3 = sample(c(likert_levels, NA), 150, replace = TRUE),
    q4 = sample(c(likert_levels, NA), 150, replace = TRUE),
    q5 = sample(c(likert_levels, NA), 150, replace = TRUE),
    q6 = sample(c(likert_levels, NA), 150, replace = TRUE)
  ) |>
  mutate(across(-grouping, ~ factor(.x, levels = likert_levels)))



p = gglikert(df%>%select(-grouping))

p$layers[[2]]$geom_params$width = parameter
p
p2 = gglikert(df%>%select(-c(grouping,q4,q5,q6)))
p2$layers[[2]]$geom_params$width = parameter
p2

enter image description here

enter image description here


Solution

  • Use the width parameter directly as per vignette

    ```{r likert1, echo=FALSE}
    gglikert(df%>%select(-grouping), width = parameter)
    ```
    

    I would not go under 0.2 though (without reducing the label size).


    Add

    Since this width parameter seems to scale with the amount of questions, we need to adjust it to achieve a constant height between likert plots:

    Code

    ---
    title: "Untitled"
    output: pdf_document
    date: "2025-03-01"
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    ```
    
    
    ```{r prep}
    library(ggstats)
    library(dplyr)
    library(ggplot2)
    
    parameter = 0.016 # per q
    
    likert_levels <- c(
      "Strongly disagree",
      "Disagree",
      "Neither agree nor disagree",
      "Agree",
      "Strongly agree"
    )
    set.seed(42)
    df <-
      tibble(
        grouping = sample(c(LETTERS[1:9]), 150, replace = TRUE),
        q1 = sample(c(likert_levels, NA), 150, replace = TRUE),
        q2 = sample(c(likert_levels, NA), 150, replace = TRUE),
        q3 = sample(c(likert_levels, NA), 150, replace = TRUE),
        q4 = sample(c(likert_levels, NA), 150, replace = TRUE),
        q5 = sample(c(likert_levels, NA), 150, replace = TRUE),
        q6 = sample(c(likert_levels, NA), 150, replace = TRUE)
      ) |>
      mutate(across(-grouping, ~ factor(.x, levels = likert_levels)))
    
    plot_likert_scale_height <- function(dat, paramater){
      q_count <- length(colnames(dat))
      gglikert(dat, width = parameter * q_count, labels_size = 2)
    }
    ```
    
    ## Likert Plots
    
    ```{r plot_likerts, echo=FALSE}
    plot_likert_scale_height(df%>%select(-grouping))
    plot_likert_scale_height(df%>%select(-c(grouping,q4,q5,q6)))
    ```
    

    Result

    out