Search code examples
rplotsurvival-analysissurvminer

How do you flip the coordinates in ggsurvplot?


I am trying to flip x and y for my survival analysis plot (using the survival/survminer package), but when I add coord_flip() to this line of code:

ggsurvplot(poop_fit, data = egg.data, pval = TRUE, conf.int = TRUE, coord_flip())

I get this error:

Error in .apply_surv_func(df, fun = fun) : Invalid 'fun' argument

Does anyone know if there is another way to flip the coordinates in survival analysis?


Solution

  • You can access/manipulate the plot by accessing it using '$plot', e.g. ggsurvplot(fit, data = lung)$plot. For example:

    require("survival")
    require("survminer")
    
    fit<- survfit(Surv(time, status) ~ sex, data = lung)
    
    ggplot1 <- ggsurvplot(fit, data = lung)$plot
    df1 <- data.frame(time=fit$time, nRisk=fit$n.risk, nRiskRel=fit$n.risk/max(fit$n.risk))  
    ggplot1 + geom_point(aes(x=time, y=nRiskRel), data = df1, alpha=0.5, size=3)
    ggplot1 + coord_flip()
    

    Pre-flipped (no + coord_flip()):

    pre-flipped.png

    Post-flipped (with + coord_flip()):

    post-flipped