透明背景的Kaplan Meier曲线(带ggsurvplot)

jvlzgdj9  于 2023-01-15  发布在  其他
关注(0)|答案(1)|浏览(171)

我用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

我想使整个背景透明,然后保存它与高品质使用它的海报。
我尝试在ggsurvplot()函数中添加以下代码行:但它不起作用

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)

有人能帮我把背景变透明吗?
谢谢大家!

qqrboqgw

qqrboqgw1#

我想我已经知道它是如何工作的了,也知道了确保它与ggsavegrid.drawggsurvplot结合在一起的复杂性。关键是,这些行位于ggsurvplot函数的ggtheme = theme(...)调用中:

library(survival)
library(survminer)

fit<- survfit(Surv(time, status) ~ sex, data = lung)

surv_plot_out <- ggsurvplot(fit, data = lung,
           legend.title = "Sex",
           legend.labs = c("Male", "Female"),
           conf.int = TRUE,
           risk.table = TRUE,
           tables.height = 0.2,
           tables.theme = theme_cleantable(),
           palette = c("#E7B800", "#2E9FDF"),
           ggtheme = theme(
             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
           ) # Change ggplot2 theme
) 

grid.draw.ggsurvplot <- function(x) survminer:::print.ggsurvplot(x, newpage = FALSE)

然后保存为png以给予透明图像:

ggsave("test_survplot.png", surv_plot_out, bg = "transparent")  
#> Saving 7 x 5 in image

结果是:

相关问题