Skip to content

Commit

Permalink
added plotting method for reg and uvreg objects (ddsjoberg#825)
Browse files Browse the repository at this point in the history
* added plotting method for reg and uvreg objects

* Update plot.R

* doc updates
  • Loading branch information
ddsjoberg authored Mar 25, 2021
1 parent 1b8a152 commit a9a9e99
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 2 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: gtsummary
Title: Presentation-Ready Data Summary and Analytic Result
Tables
Version: 1.3.7.9008
Version: 1.3.7.9009
Authors@R:
c(person(given = "Daniel D.",
family = "Sjoberg",
Expand Down Expand Up @@ -85,6 +85,7 @@ Suggests:
effectsize,
flextable (>= 0.5.10),
geepack,
GGally (>= 2.1.0),
Hmisc,
huxtable (>= 5.0.0),
kableExtra,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ S3method(inline_text,tbl_survival)
S3method(inline_text,tbl_svysummary)
S3method(inline_text,tbl_uvregression)
S3method(knit_print,gtsummary)
S3method(plot,tbl_regression)
S3method(plot,tbl_uvregression)
S3method(print,gtsummary)
S3method(tbl_regression,brmsfit)
S3method(tbl_regression,default)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# gtsummary (development version)

* Adding coefficient `plot()` methods for `tbl_regression()` and `tbl_uvregression()`

* Bug fix for spanning headers with kableExtra output. The spanning header was misplaced when the header text was sandwhiched between two blank spanning headers.

* Added _The Quarterly Journal of Economics_ to `theme_gtsummary_journal()`. This journal theme will be updated again after the gt package updates `cols_merge()` with a rows argument and allows for line breaks within cell.
Expand Down
46 changes: 46 additions & 0 deletions R/plot.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#' Plot Regression Coefficients
#'
#' The `plot()` function extracts `x$table_body` and passes the it to
#' `GGally::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 `GGally::ggcoef_plot(...)`
#'
#' @return a ggplot
#' @name plot
#'
#' @examples
#' if (requireNamespace("GGally"))
#' 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, ...) {
assert_package("GGally", fn = "plot.tbl_regression()", version = "2.1.0")

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 %>%
GGally::ggcoef_plot(exponentiate = x$inputs$exponentiate, ...)
}

#' @rdname plot
#' @export
plot.tbl_uvregression <- plot.tbl_regression
2 changes: 1 addition & 1 deletion R/tbl_strata.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ nest_df_and_svy <- function(data, strata) {
tibble(strata_var = pluck(data, "variables", strata) %>% unique()) %>%
rowwise() %>%
mutate(
data = data[data$variables[[.env$strata]] %in% strata_var, ] %>% list()
data = data[data$variables[[.env$strata]] %in% .data$strata_var, ] %>% list()
) %>%
ungroup() %>%
set_names(c(strata, "data"))
Expand Down
42 changes: 42 additions & 0 deletions man/plot.Rd

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

1 change: 1 addition & 0 deletions pkgdown/_pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ reference:
- add_q
- add_significance_stars
- add_vif
- plot

- title: Inline Reporting
- contents:
Expand Down

0 comments on commit a9a9e99

Please sign in to comment.