# How to plot the Hazard Ratio + CI over time of survival data in ggplot in R?

## Issue

**Background**

I want to plot the hazard ratio over time, including its confidence intervals, of a survival dataset. As an example, I will take a simplified dataset from the `survival`

package: the colon dataset.

```
library(survival)
library(tidyverse)
# Colon survival dataset
data <- colon %>%
filter(etype == 2) %>%
select(c(id, rx, status, time)) %>%
filter(rx == "Obs" | rx == "Lev+5FU") %>%
mutate(rx = factor(rx))
```

The dataset contains patients that received a treatment (i.e., "Lev+5FU") and patients that did not (i.e., "Obs"). The survival curves are as follows:

```
fit <- survfit(Surv(time, status) ~ rx, data = data )
plot(fit)
```

**Attempt**

Using the `cox.zph`

function, you can plot the hazard ratio of a cox model.

```
cox <- coxph(Surv(time, status) ~ rx, data = data)
plot(cox.zph(cox))
```

However, I want to plot the hazard ratio including 95% CI for this survival dataset using `ggplot`

.

**Question(s)**

- How do you extract the hazard ratio data and the 95% CIs from this cox.zph object to plot them in
`ggplot`

? - Are there other
`R`

packages that enable doing the same in a more convenient way?

## Solution

The `survminer`

package will do this for you:

```
library(survminer)
ggcoxzph(cox.zph(cox))
```

Answered By – MånsT

**This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 **