I created this survival plot (dataset=lung) with ggsurvfit which uses ggplot2.
library(survival)
library(ggplot2)
library(ggsurvfit)
p <- survfit2(Surv(time, status)~sex, data=lung)
p %>%
ggsurvfit(type = "survival", linewidth=1) +
labs(
title = "Kaplan-Meier Curve",
y = "Survival probability",
x = "Time, days") +
coord_cartesian(xlim=c(0, 100)) +
add_legend_title("Sex") +
scale_x_continuous(breaks = c(0, 25, 50, 75, 100)) +
scale_y_continuous(expand = c(0.01, 0)) +
scale_color_manual(values = c('violetred2',
'dodgerblue2')) +
scale_fill_manual(values = c('violetred2',
'dodgerblue2')) +
theme_gray() +
theme(legend.position = "bottom") +
add_censor_mark() +
add_confidence_interval() +
add_risktable(risktable_height = 0.25,
theme =
list(
theme_risktable_boxed(axis.text.y.size = 11,
plot.title.size = 11),
theme(plot.title =
element_text(face = "bold"))
)) +
#add_risktable_strata_symbol(symbol = "\U25CF", size = 10)
add_pvalue(caption="Log-rank {p.value}",
location = "annotation", x =10, y=0.15) +
add_quantile(x_value = 15, linetype = "dotted",
color = "grey30", linewidth = 0.8)
I have two problems:
Well, I think you may have several alternatives to adjust the graphs. The left side contains y-axis title which takes some space.
Here is your original graph with a new name as p_1
:
p_1 <- p %>%
ggsurvfit(type = "survival", linewidth = 1) +
labs(
title = "Kaplan-Meier Curve",
y = "Survival probability",
x = "Time, days"
) +
coord_cartesian(xlim = c(0, 100)) +
add_legend_title("Sex") +
scale_x_continuous(breaks = c(0, 25, 50, 75, 100)) +
scale_y_continuous(expand = c(0.01, 0)) +
scale_color_manual(values = c("violetred2", "dodgerblue2")) +
scale_fill_manual(values = c("violetred2", "dodgerblue2")) +
theme_gray() +
theme(legend.position = "bottom") +
add_censor_mark() +
add_confidence_interval() +
add_risktable(
risktable_height = 0.25,
theme = list(theme_risktable_boxed(
axis.text.y.size = 11,
plot.title.size = 11),
theme(
plot.title =
element_text(face = "bold")
)
)
) +
add_pvalue(
caption = "Log-rank {p.value}",
location = "annotation", x = 10, y = 0.15
) +
add_quantile(
x_value = 15, linetype = "dotted",
color = "grey30", linewidth = 0.8
)
You could change left and right side space using theme(plot.margin = )
like:
p_1 +
theme(plot.margin = margin(0, 1, 0, 0, unit = "cm"))
For your second question, you cannot do too much about it if you want to leave the legend at the bottom. You could try to use the same approach to move the risk table upwards by changing legend.box.margin
.
p_1 +
theme(legend.box.margin =margin(-.3, 0, -0.3, 0, unit = "cm"),
plot.margin = margin(0, 1, 0, 0, unit = "cm"))
It only can lift a little bit. Alternatively, just put the legend inside instead:
p_1 +
theme(plot.margin = margin(0, 1, 0, 0, unit = "cm"),
legend.position = c(0.5, 0.15),
legend.direction = "horizontal"
)