如何调整survminer::ggsurvplot轴标签与轴标题的间距?
调整ggsurvplot_combine生存曲线的轴标题间距
问题场景
使用ggsurvplot_combine生成生存曲线时,y轴标题“Survival probability”与轴标签距离过近。直接用+ theme()修改会报错,因为ggsurvplot返回的不是普通ggplot对象,而是包含多个元素的列表。
可复现代码
require(survival) require(survminer) require(ggplot2) set.seed(42) a <- c(32,291,545, 44,3,792,352, 20,615,169, 61,156, 88,863,255, 33,132,5, 63,8,964,831, 55,133, 12, 54,261,867, 17, 12,699,233,251,446, 43,223, 374, 75, 34, 69,120, 84,134,8,806,8, 48, 86,211, 2436, 61, 81, 87, 12, 838, 34, 17,141, 44,155,128,6, 29, 16, 16, 34,130,430,325, 41, 28, 53, 32,291,545, 44,3,792,352, 20,615,169, 61,156, 88,863,255, 33,132,5, 63,8,964,831, 55,133, 12, 54,261,867, 17, 12,699,233,251,446, 43,223, 86, 54,3,630, 93,699, 25,746,6, 46, 22, 60,395,402,151, 26, 38,125, 75, 34, 69,120, 84,134,8,806,8, 48, 86,211, 2436, 61, 191, 49, 59,6, 34, 56,2, 96, 422, 45, 70) b <- sample(c("Alpha", "Beta", "Gamma"), length(a), replace = T) c <- sample(0:1, length(a), replace = T) df <- data.frame(a, b, c) df$b <- factor(df$b, levels = c("Alpha", "Beta", "Gamma")) avg <- survfit(Surv(time = a, event = c) ~ 1, data = df) surv_b <- survfit(Surv(time = a, event = c) ~ b, data = df) surv_comb <- list(surv_b, avg)
解决方法
ggsurvplot_combine返回的对象是一个列表,其中plot元素才是真正的ggplot绘图对象。我们可以提取这个元素后,再叠加theme()修改轴标题的间距:
# 生成绘图对象并赋值 p <- survminer::ggsurvplot_combine( surv_comb, data = df, combine = TRUE, censor = FALSE, legend = "none", palette = c("orange", "steelblue", "seagreen", "black"), linetype = c(rep("solid",3), "dashed"), ) # 修改y轴标题与标签的间距(这里调整右边距,数值可按需修改) p$plot <- p$plot + theme(axis.title.y = element_text(margin = margin(r = 15))) # 打印修改后的图表 print(p)
如果需要调整x轴标题(Time)的间距,把axis.title.y换成axis.title.x,并调整margin的t(上边距)参数即可,比如:
p$plot <- p$plot + theme(axis.title.x = element_text(margin = margin(t = 10)))
内容的提问来源于stack exchange,提问作者Reader 123
相关产品推荐
相关产品推荐

