Skip to content

Commit

Permalink
Merge pull request #61 from larmarange/issue60
Browse files Browse the repository at this point in the history
new argument `data_fun` for `gglikert()`
  • Loading branch information
larmarange authored Apr 5, 2024
2 parents 7ca6d52 + 5023ebd commit 7cd0700
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 4 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
color (#57)
* new default value `"auto"` for `labels_color` argument in `gglikert()` and
`gglikert_stacked()` (using `hex_bw()`) (#57)
* new argument `data_fun` for `gglikert()`, `gglikert_data()` and
`gglikert_stacked()` (#60)

# ggstats 0.5.1

Expand Down
26 changes: 23 additions & 3 deletions R/gglikert.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
#' @param exclude_fill_values Vector of values that should not be displayed
#' (but still taken into account for computing proportions),
#' see [position_likert()]
#' @param data_fun for advanced usage, custom function to be applied to the
#' generated dataset at the end of `gglikert_data()`
#' @param add_labels should percentage labels be added to the plot?
#' @param labels_size size of the percentage labels
#' @param labels_color color of the percentage labels (`"auto"` to use
Expand Down Expand Up @@ -147,6 +149,13 @@
#' gglikert(df_group, q1:q6, facet_cols = vars(group))
#'
#' gglikert(df_group, q1:q6, y = "group", facet_rows = vars(.question))
#'
#' # Custom function to be applied on data
#' f <- function(d) {
#' d$.question <- forcats::fct_relevel(d$.question, "q5", "q2")
#' d
#' }
#' gglikert(df, include = q1:q6, data_fun = f)
#' }
gglikert <- function(data,
include = dplyr::everything(),
Expand All @@ -158,6 +167,7 @@ gglikert <- function(data,
sort_prop_include_center = totals_include_center,
factor_to_sort = ".question",
exclude_fill_values = NULL,
data_fun = NULL,
add_labels = TRUE,
labels_size = 3.5,
labels_color = "auto",
Expand Down Expand Up @@ -187,7 +197,8 @@ gglikert <- function(data,
sort_method = sort_method,
sort_prop_include_center = sort_prop_include_center,
factor_to_sort = {{ factor_to_sort }},
exclude_fill_values = exclude_fill_values
exclude_fill_values = exclude_fill_values,
data_fun = data_fun
)

y <- broom.helpers::.select_to_varnames(
Expand Down Expand Up @@ -363,7 +374,8 @@ gglikert_data <- function(data,
sort_method = c("prop", "mean", "median"),
sort_prop_include_center = TRUE,
factor_to_sort = ".question",
exclude_fill_values = NULL) {
exclude_fill_values = NULL,
data_fun = NULL) {
rlang::check_installed("broom.helpers")
rlang::check_installed("labelled")

Expand Down Expand Up @@ -496,6 +508,12 @@ gglikert_data <- function(data,
)
}

if (!is.null(data_fun)) {
if (!is.function(data_fun))
cli::cli_abort("{arg data_fun} should be a function.")
data <- data_fun(data)
}

data
}

Expand Down Expand Up @@ -582,6 +600,7 @@ gglikert_stacked <- function(data,
sort_method = c("prop", "mean", "median"),
sort_prop_include_center = FALSE,
factor_to_sort = ".question",
data_fun = NULL,
add_labels = TRUE,
labels_size = 3.5,
labels_color = "auto",
Expand All @@ -602,7 +621,8 @@ gglikert_stacked <- function(data,
sort_method = sort_method,
sort_prop_include_center = sort_prop_include_center,
factor_to_sort = {{ factor_to_sort }},
exclude_fill_values = NULL
exclude_fill_values = NULL,
data_fun = data_fun
)

y <- broom.helpers::.select_to_varnames(
Expand Down
15 changes: 14 additions & 1 deletion man/gglikert.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7cd0700

Please sign in to comment.