Skip to content

Commit

Permalink
feat: add plot_surv_roc and rjlegend
Browse files Browse the repository at this point in the history
  • Loading branch information
sgibb committed Mar 10, 2022
1 parent 99cfaca commit a49fe05
Show file tree
Hide file tree
Showing 10 changed files with 412 additions and 1 deletion.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: ameld
Title: Data and Model of End-Stage Liver Disease used in the AMPEL Project
Version: 0.0.15.99
Version: 0.0.16
Description:
A dataset of patients evaluated for liver transplantation at the
University Hospital Leipzig from November 2012 to June 2015.
Expand Down Expand Up @@ -28,6 +28,7 @@ Suggests:
knitr,
rmarkdown,
testthat (>= 3.0.0),
timeROC,
viridisLite,
vdiffr
License: GPL (>= 3)
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ export(observed_survival)
export(observed_vs_expected_mortality)
export(plot_dots)
export(plot_surv)
export(plot_surv_roc)
export(plot_table)
export(rcv.glmnet)
export(rjlegend)
export(which.min.error)
import(survival)
importFrom(glmnet,cv.glmnet)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

## Changes in development

## Changes in 0.0.16

- Add `main` to `plot.arcv.glmnet`.
- Add `plot_surv_roc`.
- Add `rjlegend`.

## Changes in 0.0.15

Expand Down
92 changes: 92 additions & 0 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,73 @@ plot_surv <- function(
invisible(p)
}

#' Plot survival ROC curves
#'
#' Generate ROC plots for a single timepoint for [`timeROC::timeROC()`] objects.
#'
#' @param x `list` of `timeROC::ipcwsurvivalROC` objects.
#' @param timepoint `numeric(1)`, timepoints for ROC prediction
#' @param col `character`, colours
#' @param lty `numeric`, line type
#' @param main `character(1)`, title
#' @param xlab `character(1)`, label x-axis
#' @param ylab `character(1)`, label y-axis
#' @return `double`, AUC
#'
#' @importFrom graphics abline
#' @importFrom methods is
#' @importFrom survival Surv
#' @export
plot_surv_roc <- function(x,
timepoint,
col = setNames(
palette.colors(length(x)), names(x)
),
lty = setNames(
rep.int(1, length(x)), names(x)
),
main = paste0("ROC at day ", timepoint),
xlab = "1 - Specificity",
ylab = "Sensitivity") {
requireNamespace("timeROC")
stopifnot(
all(vapply(x, is, NA, class2 = "ipcwsurvivalROC")),
all(vapply(x, function(xx)timepoint %in% xx$times, NA)),
as.logical(length(names(x))),
length(col) == length(x),
length(lty) == length(x),
length(timepoint) == 1,
length(main) == 1
)

auc <- setNames(double(length(x)), names(x))

plot(
NA,
xlim = c(0L, 1L), ylim = c(0L, 1L),
axes = FALSE, ann = FALSE, asp = TRUE
)
title(main = main, adj = 0L)
title(xlab = xlab, adj = 1L)
title(ylab = ylab, adj = 0L)

abline(0L, 1L, col = "#808080", lty = 2L)
axis(1L, lwd.ticks = 0L, col = "#808080")
axis(2L, lwd.ticks = 0L, col = "#808080")

for (i in seq(along = x)) {
j <- which(timepoint == x[[i]]$times)
auc[i] <- x[[i]]$AUC[j]
lines(x[[i]]$FP[, j], x[[i]]$TP[, j], col = col[i], lty = lty[i])
}
o <- order(auc, decreasing = TRUE)
rjlegend(
legend = sprintf("AUC %s: %0.3f", names(x)[o], auc[o]),
col = col[o], lty = lty
)
auc
}

#' Plot a table
#'
#' Plot a table on the current graphic device. Useful for risk tables.
Expand Down Expand Up @@ -192,6 +259,31 @@ plot_table <- function(
)
}

#' Legend with right justified text
#'
#' Plots a legend similar to [`legend()`] but with right justified text.
#'
#' @param x position, see [`legend()`].
#' @param y position, see [`legend()`].
#' @param legend legend text, see [`legend()`].
#' @param col colours, see [`legend()`].
#' @param lwd line width, see [`legend()`].
#' @param bty border type, see [`legend()`].
#' @param ... params passed to [`legend()`].
#' @return see [`legend()`]
#' @importFrom graphics legend strwidth text
#' @export
rjlegend <- function(x = "bottomright", y = NULL, legend, col,
lwd = 1, bty = "n", ...) {
lgd <- legend(
x = x, y = y, col = col, lwd = lwd, bty = bty,
legend = rep(" ", length(legend)), text.width = max(strwidth(legend)),
yjust = 1, xjust = 1, ...
)
text(lgd$rect$left + lgd$rect$w, lgd$text$y, labels = legend, pos = 2L)
invisible(lgd)
}

#' helper function to plot error bars
#'
#' @param x `double`, x coordinates.
Expand Down
37 changes: 37 additions & 0 deletions man/plot_surv_roc.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions man/rjlegend.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions tests/testthat/_snaps/plot/plot-surv-roc.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
74 changes: 74 additions & 0 deletions tests/testthat/_snaps/plot/rjlegend-br.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit a49fe05

Please sign in to comment.