Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

ggplot2

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

like image 396
jcruzer Avatar asked Oct 22 '25 23:10

jcruzer


2 Answers

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

like image 101
Ben Avatar answered Oct 24 '25 12:10

Ben


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

like image 35
TarJae Avatar answered Oct 24 '25 12:10

TarJae



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!