feat: new likert plot

This commit is contained in:
Andreas Gammelgaard Damsbo 2026-03-30 20:17:13 +02:00
commit ba03109416
No known key found for this signature in database
2 changed files with 64 additions and 2 deletions

50
R/plot_likert.R Normal file
View file

@ -0,0 +1,50 @@
#' Nice horizontal bar plot centred on the central category
#'
#' @returns ggplot2 object
#' @export
#'
#' @name data-plots
#'
#' @examples
#' mtcars |> plot_likert(pri = "carb", sec = "cyl")
#' mtcars |> plot_likert(pri = "carb", sec = "cyl", ter="am")
#' mtcars |> plot_likert(pri = "cyl",color.palette="Blues")
#' mtcars |> plot_likert(pri = "carb", sec = NULL,color.palette="Magma")
#' mtcars |> plot_likert(pri = "carb", sec = c("cyl","am"),color.palette="Viridis")
plot_likert <- function(data,
pri,
sec = NULL,
ter = NULL,
color.palette = "viridis") {
if (!is.null(ter)) {
ds <- split(data, data[ter])
} else {
ds <- list(data)
}
out <- lapply(ds, \(.x) {
.x[c(pri, sec)] |>
# na.omit() |>
plot_likert_single(color.palette = color.palette)
})
wrap_plot_list(out, title = glue::glue(i18n$t("Grouped by {get_label(data,ter)}")))
}
plot_likert_single <- function(data, color.palette = "viridis") {
ggstats::gglikert(data = data) +
scale_fill_generate(palette=color.palette)+
ggplot2::theme(
# legend.position = "none",
# panel.grid.major = element_blank(),
# panel.grid.minor = element_blank(),
# axis.text.y = ggplot2::element_blank(),
# axis.title.y = ggplot2::element_blank(),
text = ggplot2::element_text(size = 12)
# axis.text = ggplot2::element_blank(),
# plot.title = element_blank(),
# panel.background = ggplot2::element_rect(fill = "white"),
# plot.background = ggplot2::element_rect(fill = "white"),
# panel.border = ggplot2::element_blank()
)
}