如何在ggcuminc/survfit绘图中将风险表标题与文本左对齐
解决风险表(risktable)左对齐问题
要让风险表的标题和所有文本完全左对齐,只需在add_risktable()函数里指定风险表的主题样式、调整水平对齐参数即可,同时修正你代码里的笔误(axis.test.y应为axis.text.y)。修改后的完整代码如下:
library(survfit) library(tidycmprsk) fit.mh.male <- cuminc(Surv(time, status) ~ Exposure, data = timesplit.mh.m) fit.mh.male %>% ggcuminc()+ add_risktable( risktable_stats = `n.risk`, stats_label = list(n.risk = "No. at Risk"), size = 3, # 关键设置:让风险表内容左对齐 theme = theme_risktable(hjust = 0) )+ add_confidence_interval()+ scale_ggsurvfit()+ labs(title = "FTMH male", x = "Time since surgery")+ # 修正笔误:axis.test.y → axis.text.y theme(axis.text.y = element_text(hjust = 1))+ coord_cartesian(xlim = c(0, 10), ylim = c(0, 0.04)) + scale_color_jama()+ scale_fill_jama()+ theme_minimal()+ theme(legend.position = "bottom")+ guides(color = guide_legend(nrow = 1))
核心说明
theme = theme_risktable(hjust = 0):这个参数直接控制风险表内所有元素(标题、风险数值)的水平对齐,hjust = 0实现完全左对齐,能解决time=0处数值被覆盖、标题与内容不对齐的问题。- 修正
axis.test.y拼写错误:原代码的笔误会导致y轴文本的对齐设置失效。
内容的提问来源于stack exchange,提问作者Biromnie
相关产品推荐
相关产品推荐

