应用theme_tq导致survminer生成的表格子组颜色消失
解决theme_tq导致生存表格子组颜色变黑的问题
问题原因
你添加的scale_color_tq()和scale_fill_tq()会强制替换survminer默认的子组颜色映射为tidyquant的配色方案,当分组变量的匹配逻辑不对应时,就会默认显示黑色。theme_tq()本身只负责主题样式(字体、布局等),不会直接修改颜色。
解决方案1:保留原有子组颜色,仅应用theme_tq样式
直接移除scale_color_tq()和scale_fill_tq(),只保留theme_tq()的主题设置,就能保留survminer默认的子组颜色:
g2_table <- g2$table + theme_tq(base_size = 20, base_family = "paper_II_font") + theme(plot.margin = unit(c(0,0.2,0.01,0.1), "cm"))+ theme(axis.text.x= element_text(face = "bold")) + theme(axis.text.y= element_text(face = "bold")) + theme(axis.title.y = element_blank()) + theme(axis.title.x = element_blank()) + theme(axis.text.x=element_blank()) + theme(panel.grid = element_blank())
解决方案2:手动指定原有颜色,同时应用theme_tq
如果你想保留theme_tq的主题,又想确保颜色完全和之前一致,可以先提取原始颜色再手动设置:
# 提取g2$table的原始子组颜色 original_colors <- ggplot_build(g2$table)$data[[1]]$colour g2_table <- g2$table + theme_tq(base_size = 20, base_family = "paper_II_font") + # 手动设置回原始颜色 scale_color_manual(values = original_colors) + scale_fill_manual(values = original_colors) + theme(plot.margin = unit(c(0,0.2,0.01,0.1), "cm"))+ theme(axis.text.x= element_text(face = "bold")) + theme(axis.text.y= element_text(face = "bold")) + theme(axis.title.y = element_blank()) + theme(axis.title.x = element_blank()) + theme(axis.text.x=element_blank()) + theme(panel.grid = element_blank())
内容的提问来源于stack exchange,提问作者RKeln91
相关产品推荐
相关产品推荐

