Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upcreating theme_survminer() #151
Comments
|
I'll create this function |
# Fit survival curves
#++++++++++++++++++++++++++++++++++++
require("survival")
fit<- survfit(Surv(time, status) ~ sex, data = lung)
# Change font size, style and color
#++++++++++++++++++++++++++++++++++++
# Change font size, style and color at the same time
# Use font.x = 14, to change only font size; or use
# font.x = "bold", to change only font face.
library(survminer)
ggsurvplot(
fit, data = lung,
main = "Survival curve",
submain = "Based on Kaplan-Meier estimates",
caption = "created with survminer",
ggtheme = theme_survminer(
font.main = c(16, "bold", "darkblue"),
font.submain = c(15, "bold.italic", "purple"),
font.caption = c(14, "plain", "orange"),
font.x = c(14, "bold.italic", "red"),
font.y = c(14, "bold.italic", "darkred"),
font.tickslab = c(12, "plain", "darkgreen")
)
) |
|
I thought about adding the theme with
but you may consider adding an overloaded |
|
Suggestion to simplify -Removing the following graphical arguments (without breaking users old scripts ): font.main = c(16, "plain", "black"),
font.submain = c(15, "plain", "black"),
font.caption = c(15, "plain", "black"),
font.x = c(14, "plain", "black"), font.y = c(14, "plain", "black"),
font.tickslab = c(12, "plain", "black"),
font.risk.table.title = c(16, "plain", "black"),
font.risk.table.subtitle = c(15, "plain", "black"),
font.risk.table.caption = c(15, "plain", "black"),
font.risk.table.x = c(14, "plain", "black"), font.risk.table.y = c(14, "plain", "black"),
font.risk.table.tickslab = c(12, "plain", "black"),
font.ncensor.plot.title = c(16, "plain", "black"),
font.ncensor.plot.subtitle = c(15, "plain", "black"),
font.ncensor.plot.caption = c(15, "plain", "black"),
font.ncensor.plot.x = c(14, "plain", "black"),
font.ncensor.plot.y = c(14, "plain", "black"),
font.ncensor.plot.tickslab = c(12, "plain", "black")These arguments can be replaced by The function For example, users can customize ggsurvplots as follow.
# Customizing during plot creation
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Use this
ggsurv <- ggsurvplot(fit,
ggtheme = theme_survminer(font.main, font.submain, ....))
# Or this (which doesn't break user old scripts. Arguments are passed to ggpar)
ggsurv <- ggsurvplot(fit,
font.main, font.submain, font.x, ....)
# Customizing after plot creation
#%%%%%%%%%%%%%%%%%%%
ggsurv <- ggsurvplot(fit)
ggpubr::ggpar(ggsurv, font.main, font.submain, font.caption, ...)
ggsurv <- ggsurvplot(fit, risk.table = TRUE)
ggsurv$table <- ggpubr::ggpar(ggsurv$table, font.main, font.submain, font.caption, ...)
print(ggsurv)Regarding + operator for object of the class ggsurvplot, I tried the following code without success:
The above code is inspired from the one used by hadley in ggplot2: "+.gg" <- function (e1, e2)
{
e2name <- deparse(substitute(e2))
if (is.theme(e1))
add_theme(e1, e2, e2name)
else if (is.ggplot(e1))
add_ggplot(e1, e2, e2name)
} |
|
Should work now, with # Fit survival curves
require("survival")
fit<- survfit(Surv(time, status) ~ sex, data = lung)
# Basicsurvival curves
require("survminer")
p <- ggsurvplot(fit, data = lung, risk.table = TRUE,
main = "Survival curve",
submain = "Based on Kaplan-Meier estimates",
caption = "created with survminer")
p
# Customizing the plots
p %+% theme_survminer(
font.main = c(16, "bold", "darkblue"),
font.submain = c(15, "bold.italic", "purple"),
font.caption = c(14, "plain", "orange"),
font.x = c(14, "bold.italic", "red"),
font.y = c(14, "bold.italic", "darkred"),
font.tickslab = c(12, "plain", "darkgreen")
)
|
|
Outstanding job! Do you think this will work with the rest of survminer's functions or only with |
|
I will update the %+% operator to handle the outputs of the other survminer functions. I will also update ggsurvplot to reduce the number of graphical arguments... |
|
Regarding the problem with + operator |
|
yes, this is already the case https://github.com/kassambara/survminer/blob/master/NAMESPACE |
|
The warning message I obtained using the
But, using |
|
Now the %+% operator works with one ggplot or any list of ggplots library(survival)
fit <- coxph(Surv(futime, fustat) ~ age + ecog.ps + rx, data=ovarian)
cox.zph.fit <- cox.zph(fit)
# plot all variables
p <- ggcoxzph(cox.zph.fit)
p %+% theme_survminer(
font.main = c(16, "bold", "darkblue"),
font.x = c(14, "bold.italic", "red"),
font.y = c(14, "bold.italic", "darkred"),
font.tickslab = c(12, "plain", "darkgreen")
) |
|
The problem with So for A + B only two solutions will work. Either there is only one overloaded + (+.A or +.B) and it will be used, or there are two methods +.A and +.B but they point into the same object. It is satisfied for ggsurv()+labs() since labs() returns an object of the class The simplest solution is to remove the class |
|
Oh, thank you!!!!!! |
|
I see that you have now |
|
As This could be either gglist , ggsurv or survminer,.... |
|
It works like a charm now |
|
I'll update the 3 functions to add the class |
|
+1 |
|
The graphical parameters - font.main, font.x, ... -, which can be passed directly to the ggsurvplot(
fit, # survfit object with calculated statistics.
data = lung, # data used to fit survival curves.
risk.table = TRUE, # show risk table.
pval = TRUE, # show p-value of log-rank test.
conf.int = TRUE, # show confidence intervals for
# point estimates of survival curves.
xlim = c(0,500), # present narrower X axis, but not affect
# survival estimates.
xlab = "Time in days", # customize X axis label.
break.time.by = 100, # break X axis in time intervals by 500.
ggtheme = theme_light(), # customize plot and risk table with a theme.
risk.table.y.text.col = T,# colour risk table text annotations.
risk.table.y.text = FALSE,# show bars instead of names in text annotations
# in legend of risk table.
ncensor.plot = TRUE, # plot the number of censored subjects at time t
conf.int.style = "step", # customize style of confidence intervals
surv.median.line = "hv", # add the median survival pointer.
legend.labs =
c("Male", "Female"), # change legend labels.
palette =
c("#E7B800", "#2E9FDF"),# custom color palettes.
main = "Survival curves", # specify the title of the plot
submain = "Based on Kaplan-Meier estimates", # the subtitle of the plot
caption = "created with survminer", # the caption of the plot
font.main = c(16, "bold", "darkblue"), # font for titles of the plot, the table and censor part
font.submain = c(15, "bold.italic", "purple"), # font for subtitles in the plot, the table and censor part
font.caption = c(14, "plain", "orange"), # font for captions in the plot, the table and censor part
font.x = c(14, "bold.italic", "red"), # font for x axises in the plot, the table and censor part
font.y = c(14, "bold.italic", "darkred"), # font for y axises in the plot, the table and censor part
font.tickslab = c(12, "plain", "darkgreen"), # font for ticklabs in the plot, the table and censor part
########## risk table #########,
risk.table.title = "Note the risk set sizes", # the title of the risk table
risk.table.subtitle = "and remember about censoring.", # the subtitle of the risk table
risk.table.caption = "source code: website.com", # the caption of the risk table
risk.table.height = 0.35, # the height of the risk table
########## ncensor plot ######
ncensor.plot.title = "Number of censorings", # as above but for the censoring plot
ncensor.plot.subtitle = "over the time.",
ncensor.plot.caption = "data available at data.com",
ncensor.plot.height = 0.35
)
Now, I think that it would be better to update the README and vignette files... |
|
|
|
Suggestion:
Examples: require("survival")
fit<- survfit(Surv(time, status) ~ sex, data = lung)
# Drawing survival curves
require("survminer")
ggsurv <- ggsurvplot(fit, data = lung, risk.table = TRUE)
# Uber platinum customized survival curves
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Survival curves
ggsurv$plot <- ggsurv$plot + labs(
title = "Survival curves", # specify the title of the plot
subtitle = "Based on Kaplan-Meier estimates", # the subtitle of the plot
caption = "created with survminer" # the caption of the plot
)
#risk table
ggsurv$table <- ggsurv$table + labs(
title = "Note the risk set sizes", # the title of the risk table
subtitle = "and remember about censoring.", # the subtitle of the risk table
caption = "source code: website.com" # the caption of the risk table
)
# ncensor plot
ggsurv$ncensor.plot <- ggsurv$ncensor.plot + labs(
title = "Number of censorings",
subtitle = "over the time.",
caption = "source code: website.com"
)
# use ggpar to change the font of one or a list of ggplots at once
ggpubr::ggpar(ggsurv,
font.title = c(16, "bold", "darkblue"), # font for titles of the plot, the table and censor part
font.subtitle = c(15, "bold.italic", "purple"), # font for subtitles in the plot, the table and censor part
font.caption = c(14, "plain", "orange"), # font for captions in the plot, the table and censor part
font.x = c(14, "bold.italic", "red"), # font for x axises in the plot, the table and censor part
font.y = c(14, "bold.italic", "darkred"), # font for y axises in the plot, the table and censor part
font.tickslab = c(12, "plain", "darkgreen"), # font for ticklabs in the plot, the table and censor part
legend = "top"
)
|




suggestions by @pbiecek :
The function theme_survminer() could be a simple wrapper of the function ggpubr::ggpar() [ http://www.sthda.com/english/rpkgs/ggpubr/reference/ggpar.html ], which can already handle many of these arguments: font.x, font.main, ....