You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言两组Kaplan-Meier生存曲线异常,未显示分组曲线求助

Troubleshooting Missing Kaplan-Meier Curves for Sex Groups

Hi there! Let's work through why your KM curves aren't showing up as two distinct lines for your sex groups. Here are the most common issues and fixes to try out:

  • Check if sex is recognized as a categorical variable
    If sex is stored as a numeric/integer column instead of a factor, survfit might not treat it as a grouping variable. First, confirm its data type with:

    str(consult3$sex)
    

    If it’s not a factor, convert it explicitly (you can adjust labels to match your study’s definitions):

    consult3$sex <- factor(consult3$sex, levels = c(1, 2), labels = c("Sex Group 1", "Sex Group 2"))
    # Re-run the survival fit after conversion
    test3 <- survfit(Surv(wave, con3) ~ sex, data = consult3)
    
  • Verify both sex groups have valid event/censorship data
    It’s possible one group has no events (all con3 = 0) or all subjects experienced the event (all con3 = 1), which could make the curve overlap or fail to render separately. Check the distribution with a cross-tab:

    table(consult3$sex, consult3$con3)
    

    Make sure each group has a mix of events and censored observations.

  • Confirm your survfit output includes two groups
    Before plotting, check if the survival model actually generated results for both sex groups by running:

    summary(test3)
    

    Look for sections labeled "Stratum" – you should see separate entries for sex=1 and sex=2. If only one stratum exists, double-check that consult3$sex has two distinct values with:

    unique(consult3$sex)
    
  • Fix color mapping in ggsurvplot
    Your current color = "#2E9FDF" sets a single color for all curves, so even if two lines exist, they’ll look identical. Instead, map color to the strata to get distinct colors:

    ggsurvplot(test3, 
               color = "strata",  # Automatically uses different colors per group
               risk.table = TRUE, 
               risk.table.y.text.col = TRUE)
    # Or specify custom colors for each group:
    ggsurvplot(test3, 
               color = c("#2E9FDF", "#E7B800"),  # One color per sex group
               risk.table = TRUE, 
               risk.table.y.text.col = TRUE)
    

Try these steps one by one – chances are one of them will get your two curves displaying properly!

内容的提问来源于stack exchange,提问作者Ellen Heidinger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 11:37:42