jcruzer
jcruzer

Reputation: 117

How do I add lines to plot to highlight points using ggsurvplot?

Code is as follows:

Data <- read.csv(file='PFS.csv', header=TRUE)
fit <- survfit(Surv(Survival, Event) ~ 1, data = Data)

p = ggsurvplot(fit,
    # surv.median.line = "hv", # Add medians survival
    surv.scale = c("percent"),
    xlab="Months",
    ylab="Progression Free Survival",
    palette= 'black',
    # change the fonts for ticks
    font.tickslab=10,
    censor.shape="+",censor.size = 4, #default is 4.5
    # Change legends: title & labels
    legend.title = "Median PFS: 4.4 months; 95 % CI [3.3-5.4]",legend = c(0.7, 0.9),
    legend.labs = c("All Patients"),
    conf.int = F,
    xlim = c(0,40), #axes.offset = FALSE,
    break.time.by = 6,
    # Add risk table
    risk.table = F,
    tables.height = 0.2,
    tables.theme = theme_cleantable(),
    fontsize = 3,
    legend.key.width=unit(0,"line"),
    axis.title.y=element_text(hjust=0.50, vjust=2,margin = margin(b = 20),face="plain", colour="black", size="14"),
    scale_y_continuous(breaks = c(0,.21,.25,.50,.75,1))
  )
p

Output: Output The desired output: (I know there are few differences in the two images, but I have mainly focused on the dotted lines leading to the 12 months and 24-month points.

enter image description here

Upvotes: 4

Views: 2116

Answers (2)

Ben
Ben

Reputation: 30474

Here is one approach demonstrating with the available lung dataset.

Create a data frame for your points of interest. In this case, times will have the time points of interest, and probs will contain the probabilities at those time points. Probabilities will be derived from the fit model.

Using plot from the ggsurvplot object, you can add geom_segment. One to draw the vertical lines, and one for the horizontal lines.

library(survival)
library(survminer)

fit <- survfit(Surv(time, status) ~ 1, data = lung)

my_times <- c(250, 500)

df <- data.frame(
  times = my_times,
  probs = summary(fit, my_times)$surv
)

p1 <- ggsurvplot(fit, data = lung)

p1$plot +
  geom_segment(data = df,
               aes(x = times, y = 0, xend = times, yend = probs), linetype = "dashed") +
  geom_segment(data = df,
               aes(x = 0, y = probs, xend = times, yend = probs), linetype = "dashed")

Plot

ggsurvplot with geom_segment lines added

Upvotes: 3

TarJae
TarJae

Reputation: 78917

you could use geom_segment. Add this two lines to your plot. This is for 12 Months. For 24 month it is analogue.

p$plot + 
geom_segment(aes(x = 12, y = 0, xend = 12, yend = 21), linetype = "dashed") +  # vertical
geom_segment(aes(x = 0, y = 21, xend = 12, yend = 21), linetype = "dashed") # horiontal

Working example:

library(survminer)
library(survival)
fit<- survfit(Surv(time, status) ~ sex, data = lung)

p <- ggsurvplot(fit, data = lung)

p$plot + geom_segment(aes(x = 250, y = 0, xend = 250, yend = 0.25), linetype = "dashed")+ 
  geom_segment(aes(x = 0, y = 0.25, xend = 200, yend = 0.25), linetype = "dashed")

enter image description here

Upvotes: 3

Related Questions