From 1500bf6813181df0c7ad3ab8a495b2b54f858b41 Mon Sep 17 00:00:00 2001 From: Joseph Larmarange Date: Tue, 1 Aug 2023 19:53:45 +0200 Subject: [PATCH] new function `ggcoef_table()` displaying a coefficient table at the right of the forest plot fix #32 --- DESCRIPTION | 2 + NAMESPACE | 1 + NEWS.md | 5 + R/ggcoef_model.R | 216 +++++++++++++++ man/ggcoef_model.Rd | 56 ++++ .../ggcoef_model/ggcoef-table-ci-pattern.svg | 262 ++++++++++++++++++ .../ggcoef-table-label-estimate.svg | 262 ++++++++++++++++++ .../ggcoef_model/ggcoef-table-mod-simple.svg | 262 ++++++++++++++++++ .../ggcoef-table-stripped-rows.svg | 248 +++++++++++++++++ .../ggcoef-table-table-header.svg | 262 ++++++++++++++++++ .../ggcoef_model/ggcoef-table-table-stat.svg | 254 +++++++++++++++++ .../ggcoef-table-table-text-size.svg | 262 ++++++++++++++++++ .../ggcoef-table-table-widths.svg | 262 ++++++++++++++++++ tests/testthat/test-ggcoef_model.R | 51 ++++ 14 files changed, 2405 insertions(+) create mode 100644 tests/testthat/_snaps/ggcoef_model/ggcoef-table-ci-pattern.svg create mode 100644 tests/testthat/_snaps/ggcoef_model/ggcoef-table-label-estimate.svg create mode 100644 tests/testthat/_snaps/ggcoef_model/ggcoef-table-mod-simple.svg create mode 100644 tests/testthat/_snaps/ggcoef_model/ggcoef-table-stripped-rows.svg create mode 100644 tests/testthat/_snaps/ggcoef_model/ggcoef-table-table-header.svg create mode 100644 tests/testthat/_snaps/ggcoef_model/ggcoef-table-table-stat.svg create mode 100644 tests/testthat/_snaps/ggcoef_model/ggcoef-table-table-text-size.svg create mode 100644 tests/testthat/_snaps/ggcoef_model/ggcoef-table-table-widths.svg diff --git a/DESCRIPTION b/DESCRIPTION index a061b69..8ba3680 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -20,9 +20,11 @@ Imports: ggplot2 (>= 3.4.0), lifecycle, magrittr, + patchwork, rlang, scales, stats, + stringr, tidyr Suggests: broom, diff --git a/NAMESPACE b/NAMESPACE index a4c2488..586b08d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -13,6 +13,7 @@ export(ggcoef_compare) export(ggcoef_model) export(ggcoef_multinom) export(ggcoef_plot) +export(ggcoef_table) export(gglikert) export(gglikert_data) export(gglikert_stacked) diff --git a/NEWS.md b/NEWS.md index d9c58e6..d2929db 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,10 @@ # ggstats (development version) +**New features** + +* new function `ggcoef_table()` displaying a coefficient table at the right + of the forest plot (#32) + **Improvements** * `gglikert()` now aligns total proportions when faceting (#28) diff --git a/R/ggcoef_model.R b/R/ggcoef_model.R index e32f3a9..669dd06 100644 --- a/R/ggcoef_model.R +++ b/R/ggcoef_model.R @@ -32,6 +32,9 @@ #' @examples #' mod <- lm(Sepal.Length ~ Sepal.Width + Species, data = iris) #' ggcoef_model(mod) +#' ggcoef_table(mod) +#' ggcoef_table(mod, table_stat = c("estimate", "ci")) +#' ggcoef_table(mod, table_text_size = 5, table_witdhs = c(1, 1)) #' #' \donttest{ #' # a logistic regression example @@ -46,6 +49,7 @@ #' #' # use 'exponentiate = TRUE' to get the Odds Ratio #' ggcoef_model(mod_titanic, exponentiate = TRUE) +#' ggcoef_table(mod_titanic, exponentiate = TRUE) #' #' # display intercepts #' ggcoef_model(mod_titanic, exponentiate = TRUE, intercept = TRUE) @@ -604,6 +608,218 @@ ggcoef_data <- function( data } +#' @describeIn ggcoef_model a variation of [ggcoef_model()] adding a table +#' with estimates, confidence intervals and p-values +#' @param table_stat statistics to display in the table, use any column name +#' returned by the tidier or `"ci"` for confidence intervals formatted +#' according to `ci_pattern` +#' @param table_header optional custom headers for the table +#' @param table_text_size text size for the table +#' @param label_estimate labeller function for estimates in the table +#' @param ci_pattern glue pattern for confidence intervals in the table +#' @param table_witdhs relative widths of the forest plot and the coefficients +#' table +#' @export +ggcoef_table <- function( + model, + tidy_fun = broom.helpers::tidy_with_broom_or_parameters, + tidy_args = NULL, + conf.int = TRUE, + conf.level = .95, + exponentiate = FALSE, + variable_labels = NULL, + term_labels = NULL, + interaction_sep = " * ", + categorical_terms_pattern = "{level}", + add_reference_rows = TRUE, + no_reference_row = NULL, + intercept = FALSE, + include = dplyr::everything(), + add_pairwise_contrasts = FALSE, + pairwise_variables = broom.helpers::all_categorical(), + keep_model_terms = FALSE, + pairwise_reverse = TRUE, + emmeans_args = list(), + significance = 1 - conf.level, + significance_labels = NULL, + show_p_values = FALSE, + signif_stars = FALSE, + table_stat = c("estimate", "ci", "p.value"), + table_header = NULL, + table_text_size = 3, + label_estimate = scales::label_number(accuracy = .1), + ci_pattern = "{conf.low}, {conf.high}", + table_witdhs = c(3, 2), + ... +) { + data <- ggcoef_data( + model = model, + tidy_fun = tidy_fun, + tidy_args = {{ tidy_args }}, + conf.int = conf.int, + conf.level = conf.level, + exponentiate = exponentiate, + variable_labels = variable_labels, + term_labels = term_labels, + interaction_sep = interaction_sep, + categorical_terms_pattern = categorical_terms_pattern, + add_reference_rows = add_reference_rows, + no_reference_row = {{ no_reference_row }}, + intercept = intercept, + include = {{ include }}, + add_pairwise_contrasts = add_pairwise_contrasts, + pairwise_variables = {{ pairwise_variables }}, + keep_model_terms = keep_model_terms, + pairwise_reverse = pairwise_reverse, + emmeans_args = emmeans_args, + significance = significance, + significance_labels = significance_labels + ) + + if (show_p_values && signif_stars) + data$add_to_label <- paste0(data$p_value_label, data$signif_stars) + if (show_p_values && !signif_stars) + data$add_to_label <- data$p_value_label + if (!show_p_values && signif_stars) + data$add_to_label <- data$signif_stars + + if (show_p_values || signif_stars) { + data$label <- forcats::fct_inorder( + factor( + paste0( + data$label, + ifelse( + data$add_to_label == "", + "", + paste0(" (", data$add_to_label, ")") + ) + ) + ) + ) + data$label_light <- forcats::fct_inorder( + factor( + paste0( + data$label_light, + ifelse( + data$add_to_label == "", + "", + paste0(" (", data$add_to_label, ")") + ) + ) + ) + ) + } + + args <- list(...) + args$data <- data + args$exponentiate <- exponentiate + + if (!"y" %in% names(args) && !"facet_row" %in% names(args)) + args$y <- "label_light" + + if (!"colour" %in% names(args) && !all(is.na(data$var_label))) { + args$colour <- "var_label" + if (!"colour_guide" %in% names(args)) { + args$colour_guide <- FALSE + } + } + + if (!"y" %in% names(args)) args$x <- "label" + if (!"facet_row" %in% names(args)) args$facet_row <- "var_label" + if (!"stripped_rows" %in% names(args)) args$stripped_rows <- TRUE + if (!"strips_odd" %in% names(args)) args$strips_odd <- "#11111111" + if (!"strips_even" %in% names(args)) args$strips_even <- "#00000000" + + coef_plot <- do.call(ggcoef_plot, args) + + if (args$stripped_rows) { + if (!"term" %in% names(data)) { + data$term <- data[[args$y]] + } + data <- data %>% + dplyr::mutate(.fill = dplyr::if_else( + as.integer(.in_order(.data$term)) %% 2L == 1, + args$strips_even, + args$strips_odd + )) + } + + # building the table + tbl_data <- data %>% + dplyr::mutate( + estimate = label_estimate(estimate), + conf.low = label_estimate(conf.low), + conf.high = label_estimate(conf.high), + p.value = p_value_label + ) + tbl_data$ci <- stringr::str_glue_data(tbl_data, ci_pattern) + tbl_data$ci[is.na(data$conf.low) & is.na(data$conf.high)] <- " " + tbl_data <- tbl_data %>% + tidyr::pivot_longer( + dplyr::any_of(table_stat), + names_to = "stat", + values_to = "value" + ) + tbl_data$stat <- factor(tbl_data$stat, levels = table_stat) + + if (!is.null(table_header) && length(table_header) != length(table_stat)) + cli::cli_abort("{.arg table_header} should have the same length as {.arg table_stat}.") # nolint + + if (is.null(table_header)) { + table_header <- table_stat + if ("estimate" %in% table_header) { + table_header[table_header == "estimate"] <- + attr(data, "coefficients_label") + } + if ("ci" %in% table_header) { + table_header[table_header == "ci"] <- + paste(scales::percent(conf.level), "CI") + } + if ("p.value" %in% table_header) { + table_header[table_header == "p.value"] <- "p" + } + + } + + table_plot <- ggplot2::ggplot(tbl_data) + + ggplot2::aes( + x = .data[["stat"]], + y = .data[[args$y]], + label = .data[["value"]] + ) + + ggplot2::geom_text(hjust = .5, vjust = .5, size = table_text_size) + + ggplot2::scale_x_discrete(position = "top", labels = table_header) + + ggplot2::scale_y_discrete( + limits = rev, + expand = ggplot2::expansion(mult = 0, add = .5) + ) + + ggplot2::facet_grid( + rows = args$facet_row, + scales = "free_y", space = "free_y", switch = "y" + ) + + ggplot2::theme_light() + + ggplot2::theme( + axis.text.x = ggplot2::element_text(face = "bold", hjust = .5), + axis.text.y = ggplot2::element_blank(), + axis.title = ggplot2::element_blank(), + strip.text = ggplot2::element_blank(), + panel.grid = ggplot2::element_blank(), + axis.ticks = ggplot2::element_blank() + ) + + if (args$stripped_rows) + table_plot <- table_plot + + geom_stripped_rows( + mapping = ggplot2::aes( + odd = .data[[".fill"]], even = .data[[".fill"]], + colour = NULL, linetype = NULL + ) + ) + + # join the plots + coef_plot + table_plot + patchwork::plot_layout(widths = table_witdhs) +} + #' @describeIn ggcoef_model plot a tidy `tibble` of coefficients #' @param data a data frame containing data to be plotted, #' typically the output of `ggcoef_model()`, `ggcoef_compare()` diff --git a/man/ggcoef_model.Rd b/man/ggcoef_model.Rd index 8427147..859d931 100644 --- a/man/ggcoef_model.Rd +++ b/man/ggcoef_model.Rd @@ -4,6 +4,7 @@ \alias{ggcoef_model} \alias{ggcoef_compare} \alias{ggcoef_multinom} +\alias{ggcoef_table} \alias{ggcoef_plot} \title{Plot model coefficients} \usage{ @@ -87,6 +88,39 @@ ggcoef_multinom( ... ) +ggcoef_table( + model, + tidy_fun = broom.helpers::tidy_with_broom_or_parameters, + tidy_args = NULL, + conf.int = TRUE, + conf.level = 0.95, + exponentiate = FALSE, + variable_labels = NULL, + term_labels = NULL, + interaction_sep = " * ", + categorical_terms_pattern = "{level}", + add_reference_rows = TRUE, + no_reference_row = NULL, + intercept = FALSE, + include = dplyr::everything(), + add_pairwise_contrasts = FALSE, + pairwise_variables = broom.helpers::all_categorical(), + keep_model_terms = FALSE, + pairwise_reverse = TRUE, + emmeans_args = list(), + significance = 1 - conf.level, + significance_labels = NULL, + show_p_values = FALSE, + signif_stars = FALSE, + table_stat = c("estimate", "ci", "p.value"), + table_header = NULL, + table_text_size = 3, + label_estimate = scales::label_number(accuracy = 0.1), + ci_pattern = "{conf.low}, {conf.high}", + table_witdhs = c(3, 2), + ... +) + ggcoef_plot( data, x = "estimate", @@ -197,6 +231,21 @@ for plotting instead of the plot} \item{y.level_label}{an optional named vector for labeling \code{y.level} (see examples)} +\item{table_stat}{statistics to display in the table, use any column name +returned by the tidier or \code{"ci"} for confidence intervals formatted +according to \code{ci_pattern}} + +\item{table_header}{optional custom headers for the table} + +\item{table_text_size}{text size for the table} + +\item{label_estimate}{labeller function for estimates in the table} + +\item{ci_pattern}{glue pattern for confidence intervals in the table} + +\item{table_witdhs}{relative widths of the forest plot and the coefficients +table} + \item{data}{a data frame containing data to be plotted, typically the output of \code{ggcoef_model()}, \code{ggcoef_compare()} or \code{ggcoef_multinom()} with the option \code{return_data = TRUE}} @@ -282,12 +331,18 @@ plot. \item \code{ggcoef_multinom()}: a variation of \code{\link[=ggcoef_model]{ggcoef_model()}} adapted to multinomial logistic regressions performed with \code{\link[nnet:multinom]{nnet::multinom()}}. +\item \code{ggcoef_table()}: a variation of \code{\link[=ggcoef_model]{ggcoef_model()}} adding a table +with estimates, confidence intervals and p-values + \item \code{ggcoef_plot()}: plot a tidy \code{tibble} of coefficients }} \examples{ mod <- lm(Sepal.Length ~ Sepal.Width + Species, data = iris) ggcoef_model(mod) +ggcoef_table(mod) +ggcoef_table(mod, table_stat = c("estimate", "ci")) +ggcoef_table(mod, table_text_size = 5, table_witdhs = c(1, 1)) \donttest{ # a logistic regression example @@ -302,6 +357,7 @@ mod_titanic <- glm( # use 'exponentiate = TRUE' to get the Odds Ratio ggcoef_model(mod_titanic, exponentiate = TRUE) +ggcoef_table(mod_titanic, exponentiate = TRUE) # display intercepts ggcoef_model(mod_titanic, exponentiate = TRUE, intercept = TRUE) diff --git a/tests/testthat/_snaps/ggcoef_model/ggcoef-table-ci-pattern.svg b/tests/testthat/_snaps/ggcoef_model/ggcoef-table-ci-pattern.svg new file mode 100644 index 0000000..97792ff --- /dev/null +++ b/tests/testthat/_snaps/ggcoef_model/ggcoef-table-ci-pattern.svg @@ -0,0 +1,262 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +day + + + + + + + + + +time + + + + + + + + + +total_bill + + + + + + +-0.5 +0.0 +0.5 +1.0 +Thur +Sun +Sat +Fri +Lunch +Dinner +Beta + + + + + +p ≤ 0.05 +p > 0.05 + + + + + + + + + + + + + + + + + + +0.0 + +0.0 +-0.7 to 0.6 +p=0.879 +0.1 +-0.5 to 0.7 +p=0.715 +-0.1 +-0.8 to 0.7 +p=0.824 + + + + + + + + + + + + + + + +0.0 + +0.1 +-0.8 to 1.0 +p=0.808 + + + + + + + + + + + + + +0.1 +0.1 to 0.1 +p<0.001 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +Beta +95% CI +p +ggcoef_table() ci_pattern + + diff --git a/tests/testthat/_snaps/ggcoef_model/ggcoef-table-label-estimate.svg b/tests/testthat/_snaps/ggcoef_model/ggcoef-table-label-estimate.svg new file mode 100644 index 0000000..ebc23dd --- /dev/null +++ b/tests/testthat/_snaps/ggcoef_model/ggcoef-table-label-estimate.svg @@ -0,0 +1,262 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +day + + + + + + + + + +time + + + + + + + + + +total_bill + + + + + + +-0.5 +0.0 +0.5 +1.0 +Thur +Sun +Sat +Fri +Lunch +Dinner +Beta + + + + + +p ≤ 0.05 +p > 0.05 + + + + + + + + + + + + + + + + + + +0% + +-5% +-65%, 56% +p=0.879 +11% +-50%, 73% +p=0.715 +-9% +-84%, 67% +p=0.824 + + + + + + + + + + + + + + + +0% + +11% +-77%, 98% +p=0.808 + + + + + + + + + + + + + +10% +9%, 12% +p<0.001 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +Beta +95% CI +p +ggcoef_table() label_estimate + + diff --git a/tests/testthat/_snaps/ggcoef_model/ggcoef-table-mod-simple.svg b/tests/testthat/_snaps/ggcoef_model/ggcoef-table-mod-simple.svg new file mode 100644 index 0000000..42010e9 --- /dev/null +++ b/tests/testthat/_snaps/ggcoef_model/ggcoef-table-mod-simple.svg @@ -0,0 +1,262 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +day + + + + + + + + + +time + + + + + + + + + +total_bill + + + + + + +-0.5 +0.0 +0.5 +1.0 +Thur +Sun +Sat +Fri +Lunch +Dinner +Beta + + + + + +p ≤ 0.05 +p > 0.05 + + + + + + + + + + + + + + + + + + +0.0 + +0.0 +-0.7, 0.6 +p=0.879 +0.1 +-0.5, 0.7 +p=0.715 +-0.1 +-0.8, 0.7 +p=0.824 + + + + + + + + + + + + + + + +0.0 + +0.1 +-0.8, 1.0 +p=0.808 + + + + + + + + + + + + + +0.1 +0.1, 0.1 +p<0.001 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +Beta +95% CI +p +ggcoef_table() mod simple + + diff --git a/tests/testthat/_snaps/ggcoef_model/ggcoef-table-stripped-rows.svg b/tests/testthat/_snaps/ggcoef_model/ggcoef-table-stripped-rows.svg new file mode 100644 index 0000000..5823242 --- /dev/null +++ b/tests/testthat/_snaps/ggcoef_model/ggcoef-table-stripped-rows.svg @@ -0,0 +1,248 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +day + + + + + + + + + +time + + + + + + + + + +total_bill + + + + + + +-0.5 +0.0 +0.5 +1.0 +Thur +Sun +Sat +Fri +Lunch +Dinner +Beta + + + + + +p ≤ 0.05 +p > 0.05 + + + + + + + + + + + + + + + + + + +0.0 + +0.0 +-0.7, 0.6 +p=0.879 +0.1 +-0.5, 0.7 +p=0.715 +-0.1 +-0.8, 0.7 +p=0.824 + + + + + + + + + + + +0.0 + +0.1 +-0.8, 1.0 +p=0.808 + + + + + + + + + + + +0.1 +0.1, 0.1 +p<0.001 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +Beta +95% CI +p +ggcoef_table() stripped_rows + + diff --git a/tests/testthat/_snaps/ggcoef_model/ggcoef-table-table-header.svg b/tests/testthat/_snaps/ggcoef_model/ggcoef-table-table-header.svg new file mode 100644 index 0000000..ba912a8 --- /dev/null +++ b/tests/testthat/_snaps/ggcoef_model/ggcoef-table-table-header.svg @@ -0,0 +1,262 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +day + + + + + + + + + +time + + + + + + + + + +total_bill + + + + + + +-0.5 +0.0 +0.5 +1.0 +Thur +Sun +Sat +Fri +Lunch +Dinner +Beta + + + + + +p ≤ 0.05 +p > 0.05 + + + + + + + + + + + + + + + + + + +0.0 + +0.0 +-0.7, 0.6 +p=0.879 +0.1 +-0.5, 0.7 +p=0.715 +-0.1 +-0.8, 0.7 +p=0.824 + + + + + + + + + + + + + + + +0.0 + +0.1 +-0.8, 1.0 +p=0.808 + + + + + + + + + + + + + +0.1 +0.1, 0.1 +p<0.001 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +A +B +C +ggcoef_table() table_header + + diff --git a/tests/testthat/_snaps/ggcoef_model/ggcoef-table-table-stat.svg b/tests/testthat/_snaps/ggcoef_model/ggcoef-table-table-stat.svg new file mode 100644 index 0000000..25c9b91 --- /dev/null +++ b/tests/testthat/_snaps/ggcoef_model/ggcoef-table-table-stat.svg @@ -0,0 +1,254 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +day + + + + + + + + + +time + + + + + + + + + +total_bill + + + + + + +-0.5 +0.0 +0.5 +1.0 +Thur +Sun +Sat +Fri +Lunch +Dinner +Beta + + + + + +p ≤ 0.05 +p > 0.05 + + + + + + + + + + + + + + + + + + + +p=0.879 +-0.7, 0.6 +p=0.715 +-0.5, 0.7 +p=0.824 +-0.8, 0.7 + + + + + + + + + + + + + + + + +p=0.808 +-0.8, 1.0 + + + + + + + + + + + + + +p<0.001 +0.1, 0.1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +p +95% CI +ggcoef_table() table_stat + + diff --git a/tests/testthat/_snaps/ggcoef_model/ggcoef-table-table-text-size.svg b/tests/testthat/_snaps/ggcoef_model/ggcoef-table-table-text-size.svg new file mode 100644 index 0000000..45b89ba --- /dev/null +++ b/tests/testthat/_snaps/ggcoef_model/ggcoef-table-table-text-size.svg @@ -0,0 +1,262 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +day + + + + + + + + + +time + + + + + + + + + +total_bill + + + + + + +-0.5 +0.0 +0.5 +1.0 +Thur +Sun +Sat +Fri +Lunch +Dinner +Beta + + + + + +p ≤ 0.05 +p > 0.05 + + + + + + + + + + + + + + + + + + +0.0 + +0.0 +-0.7, 0.6 +p=0.879 +0.1 +-0.5, 0.7 +p=0.715 +-0.1 +-0.8, 0.7 +p=0.824 + + + + + + + + + + + + + + + +0.0 + +0.1 +-0.8, 1.0 +p=0.808 + + + + + + + + + + + + + +0.1 +0.1, 0.1 +p<0.001 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +Beta +95% CI +p +ggcoef_table() table_text_size + + diff --git a/tests/testthat/_snaps/ggcoef_model/ggcoef-table-table-widths.svg b/tests/testthat/_snaps/ggcoef_model/ggcoef-table-table-widths.svg new file mode 100644 index 0000000..3614616 --- /dev/null +++ b/tests/testthat/_snaps/ggcoef_model/ggcoef-table-table-widths.svg @@ -0,0 +1,262 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +day + + + + + + + + + +time + + + + + + + + + +total_bill + + + + + + +-0.5 +0.0 +0.5 +1.0 +Thur +Sun +Sat +Fri +Lunch +Dinner +Beta + + + + + +p ≤ 0.05 +p > 0.05 + + + + + + + + + + + + + + + + + + +0.0 + +0.0 +-0.7, 0.6 +p=0.879 +0.1 +-0.5, 0.7 +p=0.715 +-0.1 +-0.8, 0.7 +p=0.824 + + + + + + + + + + + + + + + +0.0 + +0.1 +-0.8, 1.0 +p=0.808 + + + + + + + + + + + + + +0.1 +0.1, 0.1 +p<0.001 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +Beta +95% CI +p +ggcoef_table() table_widths + + diff --git a/tests/testthat/test-ggcoef_model.R b/tests/testthat/test-ggcoef_model.R index e4a5d0c..8229001 100644 --- a/tests/testthat/test-ggcoef_model.R +++ b/tests/testthat/test-ggcoef_model.R @@ -327,3 +327,54 @@ test_that("tidy_args is supported", { ) expect_equal(res$estimate, 3) }) + + +test_that("ggcoef_table()", { + skip_on_cran() + skip_if_not_installed("broom.helpers") + skip_if_not_installed("reshape") + + data(tips, package = "reshape") + mod_simple <- lm(tip ~ day + time + total_bill, data = tips) + + vdiffr::expect_doppelganger( + "ggcoef_table() mod simple", + ggcoef_table(mod_simple) + ) + + vdiffr::expect_doppelganger( + "ggcoef_table() table_stat", + ggcoef_table(mod_simple, table_stat = c("p.value", "ci")) + ) + + vdiffr::expect_doppelganger( + "ggcoef_table() table_header", + ggcoef_table(mod_simple, table_header = c("A", "B", "C")) + ) + + vdiffr::expect_doppelganger( + "ggcoef_table() table_text_size", + ggcoef_table(mod_simple, table_text_size = 5) + ) + + vdiffr::expect_doppelganger( + "ggcoef_table() label_estimate", + ggcoef_table(mod_simple, label_estimate = scales::label_percent(1)) + ) + + vdiffr::expect_doppelganger( + "ggcoef_table() ci_pattern", + ggcoef_table(mod_simple, ci_pattern = "{conf.low} to {conf.high}") + ) + + vdiffr::expect_doppelganger( + "ggcoef_table() table_widths", + ggcoef_table(mod_simple, table_witdhs = c(1, 2)) + ) + + vdiffr::expect_doppelganger( + "ggcoef_table() stripped_rows", + ggcoef_table(mod_simple, stripped_rows = FALSE) + ) + +})