Skip to content

forest plot rework#162

Open
Melkiades wants to merge 21 commits intomainfrom
160_gt_forest_rework@main
Open

forest plot rework#162
Melkiades wants to merge 21 commits intomainfrom
160_gt_forest_rework@main

Conversation

@Melkiades
Copy link
Contributor

What changes are proposed in this pull request?

  • Complete rework of forest plot function (add_forest()) as attached ggplot2 for each row.
  • Removed g_forest() and its dependencies.

Provide more detail here as needed.
#160

# Simple example ------------------------------------------------------------
trial |>
  select(age, marker, grade, response) |>
  tbl_uvregression(
    y = response,
    method = glm,
    method.args = list(family = binomial),
    exponentiate = TRUE,
    hide_n = TRUE
  ) |>
  modify_column_merge(
    pattern = "{estimate} (95% CI {ci}; {p.value})",
    rows = !is.na(estimate)
  ) |>
  modify_header(estimate = "**Odds Ratio**") |>
  add_forest(table_engine = "gt")

# Realistic example ---------------------------------------------------------
trial |>
  tbl_roche_subgroups(
    rsp = "response",
    by = "trt",
    subgroups = c("grade", "stage"),
    ~ glm(response ~ trt, data = .x) |>
      gtsummary::tbl_regression(
        show_single_row = trt,
        exponentiate = TRUE
      )
  ) |>
  add_forest(pvalue = starts_with("p.value"), table_engine = "flextable")

New generic format (inspired by gtforester @ddsjoberg)
image

More specific output:
image


Pre-review Checklist (if item does not apply, mark is as complete)

  • All GitHub Action workflows pass with a ✅
  • PR branch has pulled the most recent updates from master branch: usethis::pr_merge_main()
  • If a bug was fixed, a unit test was added.
  • Code coverage is suitable for any new functions/features (generally, 100% coverage for new code): devtools::test_coverage()
  • Request a reviewer

Reviewer Checklist (if item does not apply, mark is as complete)

  • If a bug was fixed, a unit test was added.
  • Run pkgdown::build_site(). Check the R console for errors, and review the rendered website.
  • Code coverage is suitable for any new functions/features: devtools::test_coverage()

When the branch is ready to be merged:

  • Update NEWS.md with the changes from this pull request under the heading "# cards (development version)". If there is an issue associated with the pull request, reference it in parentheses at the end update (see NEWS.md for examples).
  • All GitHub Action workflows pass with a ✅
  • Approve Pull Request
  • Merge the PR. Please use "Squash and merge" or "Rebase and merge".

Copy link
Contributor Author

@Melkiades Melkiades left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shajoezhu @BFalquet let me know what you think! I managed to get also the flextable output to work with our theme

@github-actions
Copy link
Contributor

github-actions bot commented Feb 4, 2026

Unit Tests Summary

  1 files   76 suites   1m 59s ⏱️
 76 tests  75 ✅ 0 💤 0 ❌ 1 🔥
262 runs  261 ✅ 0 💤 0 ❌ 1 🔥

For more details on these errors, see this check.

Results for commit b97659d.

♻️ This comment has been updated with latest results.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 4, 2026

Unit Test Performance Difference

Test Suite $Status$ Time on main $±Time$ $±Tests$ $±Skipped$ $±Failures$ $±Errors$
add_forest 👶 $+14.03$ $+23$ $0$ $0$ $0$
forestplot 💀 $0.04$ $-0.04$ $-1$ $-1$ $0$ $0$
modify_header_rm_md 💔 $1.13$ $+1.21$ $0$ $0$ $0$ $0$
table_roche_subgroups 👶 $+6.20$ $+19$ $0$ $0$ $0$
Additional test case details
Test Suite $Status$ Time on main $±Time$ Test Case
add_forest 👶 $+14.03$ add_forest_table_engine_flextable_works
add_hierarchical_count_row 💔 $0.97$ $+2.82$ add_hierarchical_count_row_works
forestplot 💀 $0.04$ $-0.04$ test_g_forest_works
gg_km 💔 $0.34$ $+1.44$ gg_km_works_with_default_inputs
modify_header_rm_md 💔 $1.13$ $+1.21$ strip_md_bold_works_with_gtsummary_table
table_roche_subgroups 👶 $+6.20$ tbl_roche_subgroups_works
tbl_baseline_chg 💚 $41.21$ $-12.87$ tbl_baseline_chg_works
tbl_hierarchical_rate_by_grade 💔 $3.94$ $+13.98$ tbl_hierarchical_rate_by_grade_works
tbl_survfit_quantiles 💚 $5.27$ $-4.09$ tbl_survfit_quantiles_works
theme_gtsummary_roche 💚 $4.76$ $-1.40$ theme_gtsummary_roche_works

Results for commit 798c51b

♻️ This comment has been updated with latest results.

Copy link

@BFalquet BFalquet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work but apparently the version of tidyselect was updated and using an external vector in selections was deprecated. => use NSE or all_of (there are couple of occurences). Also we get a warning in the .determine_ggplot_header and ..plot_centered_axis as well as in couple of other unmodified function because we dont use .data. add them to utils::globalVariables or use .data/.env

@Melkiades
Copy link
Contributor Author

Nice work but apparently the version of tidyselect was updated and using an external vector in selections was deprecated. => use NSE or all_of (there are couple of occurences). Also we get a warning in the .determine_ggplot_header and ..plot_centered_axis as well as in couple of other unmodified function because we dont use .data. add them to utils::globalVariables or use .data/.env

Yes I saw the first issue but if you follow it, it seems to be related to process_selectors which is a cards function... I will investigate the second issue!

Comment on lines +23 to +26
#' @param header_spaces (`integer`)\cr Spaces to add to the forest plot header to
#' visually separate the two treatment areas (`trt A\n Better` and `trt B\nBetter`).
#' It is suggested to modify manually this variable if the treatment names are long,
#' with `add_forest(..., header_spaces = 5)` or `flextable::set_header_labels(ggplot = "*")`.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think estimates of widths and so on are easy to do here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hence the suggestion to do this manually

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants