Usually a Cox-regression is achieved in R by
library(survival)
model <- coxph ( SuvivalObject ~ Covariate1 + Covariate2 + Factor1 + Factor2 , data = Dataset )
The covariates can be enclosed in other funtions:
- factors should be enclosed by factor()
- strata, which allow to adjust for a factor without getting an estimate, should be enclosed by strata()
- non-log linear continuous terms can be enclosed by
pspline()
In the latter case the model might look like
model <- coxph (SurvivalObject ~ pspline(Covariate1) + Covariate2 + factor(Factor1) + strata(Factor2) , data = Dataset )
The functional form of the covariates (including the factors) can now be plotted with
termplot(model)
Though the termplot() function fails with plotting just one covariate and leaves no cusomization.
The function plotHR() plots the functional form of the desired term: plotHR(model)
plots the first term in the model by default but other terms can be accessed by calling their number (e.g. the second one):
plotHR(model , terms = 2)

In order to use the function you have to “source” it into R. It is the same procedure as calling a package, but using “source” instead of “library”.
Paste the function syntax into a textfile and safe it (as plot.HR.R) on your harddisk, remember the path and include
source("C:Path/to/plotHR_0.5.R")
before using the function.
Download plotHR()
Note: I have rewritten the function several times since I wrote the initial post … using version numbers now …
- V0.5 – bug fix for the y-scale and slight adjustment of the default plotting colors (paler CI shade and stronger term-line)
- V0.4 – the y-scale should be logarithmic; a HR of 0.5 (50% reduced Hazard) should show the same distance from HR = 1 as a doubled Hazard (HR = 2); this is now default. The linear scale I used initially is biased in this concern (Hat-tip: Arve Ulvik, Eva Pedersen and Roy Nilsen). The option y.log allows both ways (linear and log-scale); the axis labels denote Hazard Ratio instead of log(HR).
Usage:
plotHR(model , terms = 1 , se = TRUE , rug = TRUE , x.log = FALSE , y.log = TRUE , xlab = "" , ylab = "Hazard Ratio" , main = NULL , xlim = NULL , ylim = NULL, col.term = "#1F78B4", lwd.term = 3, col.se = "#A6CEE3", cex = 1 , axes = TRUE)
- model – a coxph model
- terms – integer; the number of the term to plot
- se – logical TRUE/FALSE; plotting the CI
- rug – logical TRUE/FALSE; rug plot at x-axis
- x.log – logical TRUE/FALSE; log-transformed exposure variable
- y.log – logical TRUE/FALSE; logarithmic y-scale
- xlab – character; x-axis label
- ylab – character; y-axis label
- main – character; main plot title
- xlim – 2×1 column vector; x-range of plot
- ylim – 2×1 column vector; y-range of plot
- col.term – color of HR-curve
- lwd.term – line width of HR-curve
- col.se – color of CI (if plotted)
- cex – numeric; size factor of labels











