15  Hazard Ratio Chart from Summary Data

Author

Chrissy h Roberts

15.1 Background

Often a user just wants to draw a quick chart of study results using only summary data. For instance, you may be looking at a table of hazard ratios and their confidence intervals and would like to visualise the results instead of working with the table.

This short script can take a vector of estimates, along with 95% CIs and will draw a chart.

15.2 Libraries

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.3     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ ggplot2   3.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.0
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

15.3 Dummy Data

Start by building a data frame using tibble. You should provide some labels (here IMD), along with an estimate (ABSOLUTE.CHANGE) and both lower (LCI) and upper (UCL) confidence limits

df <- tibble( 
            IMD = c("IMD1","IMD2","IMD3","IMD4","IMD5"),
            ABSOLUTE.CHANGE  = c(-3.1,-6.6,-1.6,-2.0,-0.9),
            LCI = c(-0.4,-4.4,0.49,-1.06,-0.2),
            UCI = c(-5.7,-8.8,-3.62,-2.9,-1.5)
)

df
# A tibble: 5 × 4
  IMD   ABSOLUTE.CHANGE   LCI   UCI
  <chr>           <dbl> <dbl> <dbl>
1 IMD1             -3.1 -0.4  -5.7 
2 IMD2             -6.6 -4.4  -8.8 
3 IMD3             -1.6  0.49 -3.62
4 IMD4             -2   -1.06 -2.9 
5 IMD5             -0.9 -0.2  -1.5 

15.4 Chart

There’s nothing clever about this chart. It uses geom_point to draw the estimates, then uses geom_errorbar to add the upper and lower confidence limits.

ggplot(df,aes(x=IMD,y=ABSOLUTE.CHANGE,colour=IMD))+
       geom_errorbar( mapping=aes(x=IMD, y=ABSOLUTE.CHANGE, ymin=LCI, ymax=UCI), width=0.1, linewidth=1)+
       geom_point(size=5)+
       geom_hline(yintercept = 0,lty=2)  +
       ylim (-10,10)

If you have some point estimates and know the sample size, but don’t have confidence intervals, then you can calculate the confidence intervals using [this method](../examples/95CI_point_estimate.qmd)