Lothlorian
Lothlorian

Reputation: 55

Survival Curve in R with survfit

I wanted to plot a survival curve using the following data. I called the data file as A.txt and the object A

A <- read.table(text = "
Time Status Group
8 1 A
8 1 A
8 1 A
9 1 A
9 1 A
9 1 A
15 0 A
15 0 A
7 1 B
7 1 B
8 1 B
9 1 B
10 1 B
10 1 B
15 0 B
15 0 B", header = TRUE)

I tried to plot a survival curve using this code:

title(main="Trial for Survival Curve")
fit <- survfit(Surv(Time, Status) ~ Group, data = A)
par(col.lab="red")
legend(10, .9, c("A", "B"), pch=c(2,3),col=2:3)
plot(fit, lty=2:3, col=2:3,lwd=5:5, xlab='Time(Days)',
  ylab='% Survival',mark.time=TRUE,mark=2:3)

I would like to put marks (triangle for A and "+" for B) every time when survival % decreases for instance at Day 7 and Day 8. I want this labeling throughout the graph, but it adds the labels only at the end of the experiment.

Upvotes: 1

Views: 1223

Answers (1)

BenBarnes
BenBarnes

Reputation: 19454

First, I'd recommend rearranging the plotting calls:

par(col.lab="red")
plot(fit, lty=2:3, col=2:3,lwd=5:5, xlab='Time(Days)',
  ylab='% Survival',mark.time=TRUE,mark=2:3)
title(main="Trial for Survival Curve")
legend(10, .9, c("A", "B"), pch=c(2,3),col=2:3)

You can add points to the survival plot with the points function. However, it looks like there's a small bug, which you can get around fairly easily:

firsty <- 1 ## Gets around bug
points(fit[1], col = 2, pch = 2) # Plots first group in fit
points(fit[2], col = 3, pch = 3) # Plots second group in fit

The points are plotted at the bottom of the "cliff" in the survival plot. enter image description here

Upvotes: 1

Related Questions