Dries
Dries

Reputation: 504

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?

Upvotes: 3

Views: 1667

Answers (2)

Vincent
Vincent

Reputation: 723

It is 2023 now and in the meantime a similar solution has been implemented into ggsurvplot itself: you can just specify axes.offset = F in the ggsurvplot call, so also no need to name it p (or anything else).

Upvotes: 2

Marco Sandri
Marco Sandri

Reputation: 24252

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.

Upvotes: 3

Related Questions