Legend of a survival plan

Hi, I am completely unfamiliar with R. This is my first attempt. I do survival, broken by age. I cannot figure out how to specify the colors for each age line and put it in the legend. Can anyone help?

require(survival)  # not loaded by default although installed by default
group <- age
kmsurvival1 <- survfit(Surv(as.numeric(time),event) ~ group)
plot(kmsurvival1, xlab="Time",ylab="Survival Probability", mark.time = F)
+4
source share
1 answer

You just need to specify a vector of flowers of the same length as the number of lines (i.e. groups) in your plot. You can do it like

N <- length(unique(group))
plot(kmsurvival1, xlab="Time",ylab="Survival Probability", mark.time = F,
col=1:N)
legend(
  "topright",
  legend=unique(group),
  col=1:N,
  horiz=FALSE,
  bty='n')

or you can manually specify the colors col=c('black','blue','red')(depending on how many colors you need).

From example in ?plot.survfit,

library(survival)
leukemia.surv <- survfit(Surv(time, status) ~ x, data = aml)
plot(leukemia.surv, lty = 2:3,col=3:4)
lLab <- gsub("x=","",names(leukemia.surv$strata))  ## legend labels
legend(
  "top",
  legend=lLab,
  col=3:4,
  lty=2:3,
  horiz=FALSE,
  bty='n')

enter image description here

+5
source

Source: https://habr.com/ru/post/1548254/


All Articles