Home > Software design >  Is there a way of indenting levels in the new "as_forest_plot" function from gtsummary/bst
Is there a way of indenting levels in the new "as_forest_plot" function from gtsummary/bst

Time:07-27

Using the excellent GTsummary package to try and create a forest plot of model coefficients. I see an experimental function "as_forest_plot" has been added which works but I can't manage to indent the levels of categorical variables. So far I have tried using the modify_column_indent function but without success, (code and output below), not sure are the names of the rows I am calling incorrect and is it not possible at all yet? My hope is to be able to separate the different variables and also some way of showing which is the reference level.

Many thanks in advance

library(titanic)
library(tidyverse, warn.conflicts = FALSE)
library(gtsummary, warn.conflicts = FALSE)
library(bstfun, warn.conflicts = FALSE)
library(magrittr, warn.conflicts = FALSE)
library(janitor, warn.conflicts = FALSE)


titanic::titanic_train %>% 
  clean_names %>% 
  select(-c(name, parch, ticket, cabin, embarked)) %>% 
  mutate(pclass = factor(pclass)) %>% 
  glm(survived ~ . - passenger_id, data = ., family = "binomial") %>%
  tbl_regression(exponentiate = TRUE) %>% 
  modify_column_indent(columns = label, rows = (header_row == TRUE)) %>%
  modify_cols_merge(
  pattern = "{estimate} ({ci})",
  rows = !is.na(estimate)
) %>%
  modify_header(estimate = "OR (95% CI)") %>%
  as_forest_plot( 
    col_names = c("estimate", "p.value"),
    col = forestplot::fpColors(box = "darkred"))

enter image description here

CodePudding user response:

Yes, but you need to manually add the spaces. Example below!

library(gtsummary)
library(bstfun)

trial %>%
  select(response, grade) %>%
  mutate(grade = paste0("     ", grade)) %>%
  tbl_uvregression(
    method = glm,
    y = response,
    method.args = list(family = binomial),
    exponentiate = TRUE
  ) %>%
  as_forest_plot()

enter image description here

  • Related