-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_km.R
More file actions
41 lines (40 loc) · 1.79 KB
/
plot_km.R
File metadata and controls
41 lines (40 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# function to plot KM from survival fit
plot_KM <- function(fit, gene, colors, labels = NULL, lty = NULL) {
plotdat <- data.frame(time = fit$time,
surv = fit$surv,
group = rep(names(fit$strata), fit$strata))
if (is.null(labels)) {
labels <- paste0(gene, " ", sub("^.+\\=(.+)$", "\\1", names(fit$strata)),
" (n=", fit$n, ")")
plotdat$group <- factor(plotdat$group, levels = names(fit$strata),
labels = labels)
} else {
plotdat$group <- factor(plotdat$group, levels = names(fit$strata),
labels = labels)
}
plotdat <- plotdat[rep(seq_len(nrow(plotdat)), each = 2), ]
plotdat$surv <- c(1, plotdat$surv[1:(length(plotdat$surv)-1)])
switchidx <- which(diff(plotdat$time)<0)+1
plotdat[c(1,switchidx),]$surv <- 1
tmp <- plotdat[c(1,switchidx),]
tmp$time <- 0
plotdat <- rbind(plotdat, tmp)
maxx <- max(plotdat$time)
xpos <- maxx - maxx/6
pval <- data.frame(x=xpos, y=0.85, label=fit$pval_txt)
if (is.null(lty)) lty <- rep(1, length(levels(plotdat$group)))
ggplot(plotdat, aes(x = time, y = surv)) +
geom_line(aes(col = group, lty = group), lwd = 1.1, alpha = .6) +
theme_classic(base_size = 14, base_family = "Arial") +
geom_text(data=pval, aes(x=x, y=y, label=label),
size = 5, family="Arial") +
scale_color_manual(values = colors, name=NULL) +
scale_y_continuous(limits = c(0,1), expand = c(0,0),
name = "OS probability") +
scale_x_continuous(expand = c(0,0), name = "Time [years]") +
scale_linetype_manual(values=lty, name=NULL) +
theme(strip.background = element_blank(),
panel.spacing = unit(1, "line"),
plot.title = element_text(size=14),
legend.position="bottom")
}