Search code examples
rggplot2survival-analysis

ggsurvplot - axes crossing at 0,0


Survminer produces nice plots, but is there a way to further change the outcome with regular ggplot-commands?

What I try to do is make the y-axis start in the origin, as stated here. For a regular ggplot, this works perfectly, but I can't make it work with survminer:

library(survival)
library(survminer)

df<-genfan
df$treat<-sample(c(0,1),nrow(df),replace=TRUE)


fit <- survfit(Surv(hours, status) ~ treat,
               data = df)

p<-ggsurvplot(fit, data = df, risk.table = TRUE,,ncensor.plot=FALSE,tables.theme = theme_cleantable(),
              ggtheme = theme_survminer(font.legend=c(12,"bold","black") ))


p%+%scale_x_continuous(expand=c(0,0))%+%scale_y_continuous(expand=c(0,0))

This produces an error

"Scale for 'y' is already present. Adding another scale for 'y', which will replace the existing scale."

and an additional error

"Error: Discrete value supplied to continuous scale"

Is there some way around this?


Solution

  • A possibile solution to your problem is to modify the ggsurvplot and ggrisktable inserting the expand=c(0,0) in the proper place.
    At this link you can find my proposal.
    Click on "download" and save the file a_modified_ggsurvplot.txt in your working directory.
    Then run the following code:

    library(survival)
    library(survminer)
    
    df <- genfan
    df$treat <- sample(c(0,1),nrow(df),replace=TRUE)
    fit <- survfit(Surv(hours, status) ~ treat, data = df)
    
    source("a_modified_ggsurvplot.txt")
    p <- myggsurvplot(fit, data = df, risk.table = TRUE,
         ncensor.plot=FALSE,tables.theme = theme_cleantable(),
         ggtheme = theme_survminer(font.legend=c(12,"bold","black")))
    print(p)
    

    Here is the plot: enter image description here

    Hope it could help you.