I have created a kaplan meier survival plot with the following code below:
data <- read.table("https://pastebin.com/raw/jEsGrNti", header = T) # sample data.frame
library(survival)
library(survminer)
# make survival plot
fit<-survfit(Surv(data$day, data$status)~data$treatment)
lev<-length(levels(data$treatment))
plot(fit, col=c("khaki3","orange","black","blue","darkgreen","hotpink","grey","29","brown","291"), yaxt="n",yaxs="r",lwd=3,cex=2,mark.time=T, main="Example Survival Plot",xlab="Time [days]",font.lab=2,frame.plot=T);axis(side=2, at=pretty(fit$surv), lab=paste0(pretty(fit$surv) * 100), las=TRUE)
mtext("Survival [%]", side=3, at=-3.5, line=0.3, font=2, cex=1)
legend(65, 0.95, levels(data$treatment), col=c("khaki3","orange","black","blue","darkgreen","hotpink","grey","29","brown","291"), lwd=3,cex = 1.5)
#test for significant differences####
M<-survdiff(Surv(data$day, data$status)~data$treatment, rho=0) # log rank test
#create pairewise table of significant differences based on above test
pvalues<-pairwise_survdiff(Surv(day, status) ~ treatment, data = data, rho = 0, p.adjust.method = "bonferroni");pvalues
# add significant letters manually based on results from above test
a<-1.2
text(22,-0.02, "C", cex=a,font = 2 , col="black")
text(70,-0.02, "B", cex=a,font = 2, col="blue")
text(45,-0.02, "A", cex=a,font = 2, col="darkgreen")
text(43,-0.02, "A", cex=a,font = 2, col="hotpink")
text(52,-0.02, "A", cex=a,font = 2, col="grey")
text(54,-0.02, "A", cex=a,font = 2, col="29")
text(14,-0.02, "C", cex=a,font = 2, col="hotpink")
text(50,-0.02, "A", cex=a,font = 2, col="291")
text(63.5,-0.02, "B", cex=a,font = 2, col="khaki3")
text(61.8,-0.02, "B", cex=a,font = 2, col="orange")
I use a sort of "back door" approach and add my significant letters manually. I was curious if someone could come up with a better way of adding the significant letters to the graph using code.
Just as my example shows: I would like the letters to be displayed below the last data point of each line, and for the letter to match the color of the line it corresponds to.