如何为ggsurvplot绘制的Kaplan-Meier曲线设置透明背景?
解决ggsurvplot设置透明背景及高质量保存问题
问题说明
使用ggsurvplot绘制Kaplan-Meier曲线后,尝试直接添加透明背景参数未生效,同时需要高质量保存用于海报制作。相关代码如下:
原绘图代码
kaplan_plot365d <- ggsurvplot(kmcurve365d, conf.int = TRUE, legend.labs = c('Trop-T normal and Trop-I normal', 'Trop-T elevated and Trop-I normal', "Trop-T elevated and Trop-I elevated"), legend.title = '', title = 'Kaplan-Meier Curve for 365-Day Mortality', xlab = 'Time in Days', ylab = 'Survival probability', main = 'Kaplan-Meier Model', ylim=c(min(0.7), max(1)), xlim=c(min(0), max(366)), break.time.by = 120, palette = c('#FDE725FF', "#21908CFF", "#440154FF"), risk.table = TRUE, risk.table.y.text = TRUE, fontsize = 4, tables.theme = clean_theme(), risk.table.title = "Survivors per group") + guides(colour = guide_legend(nrow = 2)) kaplan_plot365d
无效的透明背景参数
panel.background = element_rect(fill='transparent'), #transparent panel bg plot.background = element_rect(fill='transparent', color=NA), #transparent plot bg panel.grid.major = element_blank(), #remove major gridlines panel.grid.minor = element_blank(), #remove minor gridlines legend.background = element_rect(fill='transparent'), #transparent legend bg legend.box.background = element_rect(fill='transparent') #transparent legend panel
原保存代码
grid.draw.ggsurvplot <- function(x){ survminer:::print.ggsurvplot(x, newpage = FALSE)} ggsave("Kaplan-Meier_365d_withCI_Troponine.jpg", path = ("I:\Personal\Poster/"), plot = kaplan_plot365d, dpi = 300)
解决方案
1. 正确设置透明主题(主图+风险表)
ggsurvplot返回的是包含主生存曲线($plot)和风险表($table)的列表,需分别为两部分设置透明主题:
# 定义统一的透明主题 transparent_theme <- theme( panel.background = element_rect(fill='transparent'), plot.background = element_rect(fill='transparent', color=NA), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), legend.background = element_rect(fill='transparent'), legend.box.background = element_rect(fill='transparent') ) # 绘制并应用透明主题 kaplan_plot365d <- ggsurvplot(kmcurve365d, conf.int = TRUE, legend.labs = c('Trop-T normal and Trop-I normal', 'Trop-T elevated and Trop-I normal', "Trop-T elevated and Trop-I elevated"), legend.title = '', title = 'Kaplan-Meier Curve for 365-Day Mortality', xlab = 'Time in Days', ylab = 'Survival probability', main = 'Kaplan-Meier Model', ylim=c(min(0.7), max(1)), xlim=c(min(0), max(366)), break.time.by = 120, palette = c('#FDE725FF', "#21908CFF", "#440154FF"), risk.table = TRUE, risk.table.y.text = TRUE, fontsize = 4, tables.theme = clean_theme() + transparent_theme, # 风险表应用透明主题 risk.table.title = "Survivors per group") + guides(colour = guide_legend(nrow = 2)) + transparent_theme # 主图应用透明主题 # 额外确保风险表背景完全透明 kaplan_plot365d$table <- kaplan_plot365d$table + theme(panel.background = element_rect(fill='transparent'), plot.background = element_rect(fill='transparent', color=NA))
2. 修正保存设置
- JPG格式不支持透明通道,需改用PNG格式
ggsave需添加bg="transparent"参数,同时修正路径斜杠(Windows下用/或\\)
ggsave("Kaplan-Meier_365d_withCI_Troponine.png", path = "I:/Personal/Poster/", plot = kaplan_plot365d, dpi = 300, bg = "transparent")
内容的提问来源于stack exchange,提问作者esptang
相关产品推荐
相关产品推荐

