如何在不更改R主题的情况下在ggsurvplot中添加网格?

时间:2018-12-05 13:01:10

标签: r ggplot2

我希望在不更改ggtheme的情况下为我的ggsurvplot添加一个坚实的浅灰色网格-与ggplot()中的grids(linetype="solid")相当。

我的数据

p <- structure(list(Studie = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L), Age = c(35, 38, 67, 18, 62, 61, 31, 34, 26, 44, 33, 54, 35, 49, 62, 56, 41, 58, 65, 29,63, 63, 51, 56, 44, 45, 47, 67, 56, 54, 41, 78, 53.8, 58.3), WHO = c(1L, 3L, 2L, 2L, 2L, 3L, 2L, 3L, 1L, 2L, 3L, 3L, 3L, 1L, 2L, 1L, 2L, 3L, 3L, 1L, 2L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L), Death = c(0L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 0L), Follow.up.death = c(18,2, 14, 17, 31, 4, 20, 15, 12, 19, 10, 17, 27, 22, 3, 43, 24, 14, 13, 5, 12, 137, 22, 87, 48, 24, 72, 32, 14, 83, 57, 16, 70, 1.9)), .Names = c("Studie", "Age", "WHO", "Death", "Follow.up.death"), class = "data.frame", row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 33L, 35L, 36L, 37L))

我使用了以下脚本

fit <- survfit(Surv(p$Follow.up.death, p$Death) ~ 1 + factor(p$WHO), conf.type="log", data=p)
j <- ggsurvplot(
  fit,                     
  data = p, 
  #fun="cumhaz",
  risk.table = "abs_pct", #risk.table.col="strata",
  pval = TRUE,      
  pval.coord = c(0, 0.25),
  conf.int = F,         
  legend.labs=c("WHO-I", "WHO-II", "WHO-III"),
  cumevents.title = "Cumulative number of deaths",
  size=c(0.7,0.7,0.7,0.7),                    
  xlim = c(0,35),
  alpha=c(0.8),
  break.x.by = 5,    
  xlab="Time in months",
  ylab="Probability of overall survival",
  ggtheme = theme_classic2(),             
  risk.table.y.text.col = T,
  risk.table.y.text = TRUE, 
  surv.median.line = "v",
  ylim=c(0,1),
  cumevents=TRUE,
  palette="jco",
  surv.scale="percent")

j

谢谢C

2 个答案:

答案 0 :(得分:3)

我修改了代码。我在ggtheme中添加了一个新主题。

theme <- theme(axis.line = element_line(colour = "black"),
           panel.grid.major = element_line(colour = "grey90"),
           panel.grid.minor = element_line(colour = "grey90"),
           panel.border = element_blank(),
           panel.background = element_blank()) 

j <- ggsurvplot(
  fit,                     
  data = p, 
  #fun="cumhaz",
  risk.table = "abs_pct", #risk.table.col="strata",
  pval = TRUE,      
  pval.coord = c(0, 0.25),
  conf.int = F,         
  legend.labs=c("WHO-I", "WHO-II", "WHO-III"),
  cumevents.title = "Cumulative number of deaths",
  size=c(0.7,0.7,0.7,0.7),                    
  xlim = c(0,35),
  alpha=c(0.8),
  break.x.by = 5,    
  xlab="Time in months",
  ylab="Probability of overall survival",
  ggtheme = theme,             
  risk.table.y.text.col = T,
  risk.table.y.text = TRUE, 
  surv.median.line = "v",
  ylim=c(0,1),
  cumevents=TRUE,
  palette="jco",
  tables.theme = theme,
  surv.scale="percent")

j

enter image description here

答案 1 :(得分:0)

由于我对survminer软件包不熟悉,因此我不确定是否有“正确”的方法来执行此操作,但是您可以通过这种方法来实现:

j$plot$theme$panel.grid.major <- element_line(colour="grey90")

编辑以添加到表中

j$table$theme$panel.grid.major <- element_line(colour="grey90")
j$cumevents$theme$panel.grid.major <- element_line(colour="grey90")