Search code examples
rstatisticssurvival-analysis

R Survival curve with two groups looks weird


I have estimated a Kaplan-Meier with two group using this code:

test3 <- survfit(Surv(wave, con3) ~ sex, data = consult3)
ggsurvplot(test3, color = "#2E9FDF",
       risk.table = TRUE, risk.table.y.text.col = TRUE)

and even though it produces a KM, it looks very weird and does not show me 2 different survival curves for sex==1 or sex==2. Does anybody know what I might have done wrong? Thanks!!

1


Solution

  • It's always better to include some sample data so we can see where the problem lies. However, let's see if we can create some data that will replicate your problem:

    library(survival)
    library(survminer)
    
    set.seed(69)
    
    consult3 <- data.frame(sex = rep(1:2, each = 50),
                           con3 = c(rbinom(50, 1, 0.2), rbinom(50, 1, 0.4)),
                           wave = sample(3, 100, TRUE))
    

    Now, using your code, we can get a similar-looking result:

    test3 <- survfit(Surv(wave, con3) ~ sex, data = consult3)
    
    ggsurvplot(test3, risk.table = TRUE, color = "#2E9FDF",
               risk.table.y.text.col = TRUE)
    

    As far as I can tell, the problem with this is that you have set a single colour aesthetic. The solution is to just remove this:

    ggsurvplot(test3, risk.table = TRUE, risk.table.y.text.col = TRUE)
    

    If you want control over the colour of the lines, use palette instead of color:

    ggsurvplot(test3, palette = c("red", "forestgreen"), alpha = 0.5,
               risk.table = TRUE, risk.table.y.text.col = TRUE)
    

    enter image description here Created on 2020-09-19 by the reprex package (v0.3.0)