forked from ddsjoberg/gtsummary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.R
48 lines (44 loc) · 1.59 KB
/
plot.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#' Plot Regression Coefficients
#'
#' The `plot()` function extracts `x$table_body` and passes the it to
#' `ggstats::ggcoef_plot()` along with a formatting options.
#'
#' \lifecycle{experimental}
#' @param x 'tbl_regression' or 'tbl_uvregression' object
#' @param remove_header_rows logical indicating whether to remove header rows
#' for categorical variables. Default is `TRUE`
#' @param remove_reference_rows logical indicating whether to remove reference rows
#' for categorical variables. Default is `FALSE`.
#' @param ... arguments passed to `ggstats::ggcoef_plot(...)`
#'
#' @return a ggplot
#' @name plot
#'
#' @examplesIf broom.helpers::.assert_package("ggstats", pkg_search = "gtsummary", boolean = TRUE)
#' glm(response ~ marker + grade, trial, family = binomial) %>%
#' tbl_regression(
#' add_estimate_to_reference_rows = TRUE,
#' exponentiate = TRUE
#' ) %>%
#' plot()
NULL
#' @rdname plot
#' @export
plot.tbl_regression <- function(x,
remove_header_rows = TRUE,
remove_reference_rows = FALSE, ...) {
check_dots_empty(error = function(e) inform(c(e$message, e$body)))
assert_package("ggstats", fn = "plot.tbl_regression()")
df_coefs <- x$table_body
if (isTRUE(remove_header_rows)) {
df_coefs <- df_coefs %>% filter(!.data$header_row %in% TRUE)
}
if (isTRUE(remove_reference_rows)) {
df_coefs <- df_coefs %>% filter(!.data$reference_row %in% TRUE)
}
df_coefs %>%
ggstats::ggcoef_plot(exponentiate = x$inputs$exponentiate, ...)
}
#' @rdname plot
#' @export
plot.tbl_uvregression <- plot.tbl_regression