ggsurvplot扩展坐标轴后绘图与风险表对齐问题求助
问题
用ggsurvplot绘制Kaplan-Meier生存曲线并生成风险人数表时,已对绘图坐标轴做了扩展设置,但风险表和主图无法对齐,需要同步调整风险表的坐标轴实现两者对齐。代码示例如下:
library(survival) library(survminer) library(ggpp) df <- lung # 转换生存时间为月份 df$time_months <- df$time/(365.25/12) km_fit<- survfit(Surv(time_months, status) ~ sex, data = df) time_int <- 2 # 绘制生存曲线 km_plot <- ggsurvplot(km_fit, data = df, risk.table = TRUE, fontsize = 3, cumevents = FALSE, risk.table.height = 0.18, cumevents.height = 0.18, break.x.by = time_int, # 设置X轴刻度间隔为X个月 x.tick.by = time_int, # 设置X轴刻度标记间隔为X个月 time.inc = time_int, # 设置风险表分组间隔为X个月 risk.table.by = time_int, # 设置风险表分组间隔为X个月 legend.title = "", # 清空图例标题 tables.y.text = FALSE, # 风险表仅保留颜色标识,移除文字 surv.median.line = "hv", # 添加中位生存时间线 xlim = c(0, max(km_fit$time*1.2)), xlab = "Time (months)") # 自定义绘图样式 km_plot$table <- km_plot$table + theme(plot.title = element_text(size = 10, color = "black", face = "bold")) km_plot$cumevents <- km_plot$cumevents + theme(plot.title = element_text(size = 10, color = "black", face = "bold")) km_plot$table$theme$axis.text.y$size <- 8 km_plot$cumevents$theme$axis.text.y$size <- 8 km_plot$table$theme$axis.text.x$size <- 8 km_plot$cumevents$theme$axis.text.x$size <- 8 km_plot$table$theme$axis.title.x$size <- 8 km_plot$cumevents$theme$axis.title.x$size <- 8 km_plot$plot <- km_plot$plot + scale_y_continuous(expand = c(0,0), breaks = seq(0,1,by=0.1), labels = seq(0,100,by=10), minor_breaks = seq(0, 1, 0.1)) + scale_x_continuous(expand = c(0,0), breaks = seq(0, round(max(km_plot$data.survplot$time*1.2)), time_int), minor_breaks = seq(0, round(max(km_plot$data.survplot$time*1.2)), time_int / time_int)) + theme(panel.grid.minor.y = element_line(color = "grey80", linewidth = 0.5, linetype = 1), panel.grid.minor.x = element_line(color = "grey80", linewidth = 0.5, linetype = 1), panel.grid.major.y = element_line(color = "grey80", linewidth = 0.5, linetype = 1), panel.grid.major.x = element_line(color = "grey80", linewidth = 0.5, linetype = 1)) + theme(legend.text = element_text(size = 12, color = "black", face = "bold")) km_plot
问题效果图:
解决方案
核心问题是仅单独调整了主图的x轴范围和扩展设置,未同步更新风险表的对应配置。只需给风险表添加与主图完全一致的scale_x_continuous设置即可实现对齐,具体操作如下:
- 在修改主图样式的代码后,添加风险表的x轴同步代码:
# 同步风险表的x轴配置,与主图保持一致 km_plot$table <- km_plot$table + scale_x_continuous( expand = c(0, 0), breaks = seq(0, round(max(km_plot$data.survplot$time*1.2)), time_int), minor_breaks = seq(0, round(max(km_plot$data.survplot$time*1.2)), time_int / time_int), limits = c(0, round(max(km_plot$data.survplot$time*1.2))) )
- 清理无效代码:由于你设置了
cumevents = FALSE,代码中关于km_plot$cumevents的样式修改不会生效,可直接删除这部分冗余代码。
修改后的完整代码:
library(survival) library(survminer) library(ggpp) df <- lung # 转换生存时间为月份 df$time_months <- df$time/(365.25/12) km_fit<- survfit(Surv(time_months, status) ~ sex, data = df) time_int <- 2 # 绘制生存曲线 km_plot <- ggsurvplot(km_fit, data = df, risk.table = TRUE, fontsize = 3, cumevents = FALSE, risk.table.height = 0.18, break.x.by = time_int, # 设置X轴刻度间隔为X个月 x.tick.by = time_int, # 设置X轴刻度标记间隔为X个月 time.inc = time_int, # 设置风险表分组间隔为X个月 risk.table.by = time_int, # 设置风险表分组间隔为X个月 legend.title = "", # 清空图例标题 tables.y.text = FALSE, # 风险表仅保留颜色标识,移除文字 surv.median.line = "hv", # 添加中位生存时间线 xlim = c(0, max(km_fit$time*1.2)), xlab = "Time (months)") # 自定义风险表标题样式 km_plot$table <- km_plot$table + theme(plot.title = element_text(size = 10, color = "black", face = "bold")) # 调整风险表文字大小 km_plot$table$theme$axis.text.y$size <- 8 km_plot$table$theme$axis.text.x$size <- 8 km_plot$table$theme$axis.title.x$size <- 8 # 自定义主图样式 km_plot$plot <- km_plot$plot + scale_y_continuous(expand = c(0,0), breaks = seq(0,1,by=0.1), labels = seq(0,100,by=10), minor_breaks = seq(0, 1, 0.1)) + scale_x_continuous(expand = c(0,0), breaks = seq(0, round(max(km_plot$data.survplot$time*1.2)), time_int), minor_breaks = seq(0, round(max(km_plot$data.survplot$time*1.2)), time_int / time_int)) + theme(panel.grid.minor.y = element_line(color = "grey80", linewidth = 0.5, linetype = 1), panel.grid.minor.x = element_line(color = "grey80", linewidth = 0.5, linetype = 1), panel.grid.major.y = element_line(color = "grey80", linewidth = 0.5, linetype = 1), panel.grid.major.x = element_line(color = "grey80", linewidth = 0.5, linetype = 1)) + theme(legend.text = element_text(size = 12, color = "black", face = "bold")) # 同步风险表的x轴配置,与主图保持一致 km_plot$table <- km_plot$table + scale_x_continuous( expand = c(0, 0), breaks = seq(0, round(max(km_plot$data.survplot$time*1.2)), time_int), minor_breaks = seq(0, round(max(km_plot$data.survplot$time*1.2)), time_int / time_int), limits = c(0, round(max(km_plot$data.survplot$time*1.2))) ) km_plot
内容的提问来源于stack exchange,提问作者krtbris
相关产品推荐
相关产品推荐

