rsurvival-analysiscox-regressionhazard

How to create an aesthetically pleasant Hazard Ratio Chart in R


Found This Online

I was looking at a Youtube video I found online. I'm new to survival analysis. The host mentioned that the second graph was created using a mixture of packages Broom & ggplot2.

Any ideas?

# Current Code:
sigMod = coxph(Surv(time, DEATH_EVENT) ~ age+anaemia+creatinine_phosphokinase+ejection_fraction+
                serum_creatinine+hypertension, data=HF)

ggforest(sigMod, data = HF)

EDIT 1

Added code so far:

tidy(sigMod) %>% select(term, estimate) %>%
  ggplot(aes(x=estimate, y=term)) + geom_boxplot()

EDIT 2

My Model's data after using Broom: | Term | Estimate | |---------------------|------------------| | Age | 0.0436065795 | | Anaemia1 | 0.3932590155 | | creatinine_phosphokinase | 0.0001964616 | | ejection_fraction | -0.0517850968 | | serum_creatinine | 0.3483455436 | | hypertensionPresent | 0.4667523759 |


Solution

  • Here's a fully reproducible example of how something like your target plot could be achieved, using the pbc dataset from the survival package. Just swap in your own coxph call at the start:

    library(survival)
    library(tidyverse)
    library(broom)
    
    coxph(Surv(time, status) ~ sex + ascites + spiders + hepato + edema, 
          data = pbc) %>%
      tidy() %>%
      mutate(upper = estimate + 1.96 * std.error,
             lower = estimate - 1.96 * std.error) %>%
      mutate(across(all_of(c("estimate", "lower", "upper")), exp)) %>%
      ggplot(aes(estimate, term, color = estimate > 1)) +
      geom_vline(xintercept = 1, color = "gray75") +
      geom_linerange(aes(xmin = lower, xmax = upper), size = 1.5, alpha = 0.5) +
      geom_point(size = 4) +
      theme_minimal(base_size = 16) +
      scale_color_manual(values = c("green4", "red3"), guide = "none") +
      xlim(c(0, 5)) +
      labs(title = "Hazard ratio for various clinical findings in PBC", y = NULL,
           x = "Hazard ratio estimate (95% Confidence Intervals)") +
      theme(axis.text.y = element_text(hjust = 0, size = 18))
    

    enter image description here