@@ -115,11 +115,10 @@ jskm <- function(sfit,
115
115
showpercent = F ,
116
116
status.cmprsk = NULL ,
117
117
linewidth = 0.75 ,
118
- theme = NULL ,
119
- nejm.infigure.ratiow = 0.6 ,
120
- nejm.infigure.ratioh = 0.5 ,
121
- nejm.infigure.ylim = c(0 ,1 ),
122
-
118
+ theme = NULL ,
119
+ nejm.infigure.ratiow = 0.6 ,
120
+ nejm.infigure.ratioh = 0.5 ,
121
+ nejm.infigure.ylim = c(0 , 1 ),
123
122
... ) {
124
123
# ################################
125
124
# sorting the use of subsetting #
@@ -128,7 +127,7 @@ jskm <- function(sfit,
128
127
n.risk <- n.censor <- surv <- strata <- lower <- upper <- NULL
129
128
130
129
times <- seq(0 , max(sfit $ time ), by = timeby )
131
- if (! is.null(theme )&& theme == ' nejm' ) legendposition <- ' right'
130
+ if (! is.null(theme ) && theme == " nejm" ) legendposition <- " right"
132
131
if (is.null(subs )) {
133
132
if (length(levels(summary(sfit )$ strata )) == 0 ) {
134
133
subs1 <- 1
@@ -158,43 +157,43 @@ jskm <- function(sfit,
158
157
subs2 <- which(regexpr(ssvar , summary(sfit , censored = T )$ strata , perl = T ) != - 1 )
159
158
subs3 <- which(regexpr(ssvar , summary(sfit , times = times , extend = TRUE )$ strata , perl = T ) != - 1 )
160
159
}
161
-
160
+
162
161
if (! is.null(subs ) | ! is.null(sfit $ states )) pval <- FALSE
163
-
162
+
164
163
# #################################
165
164
# data manipulation pre-plotting #
166
165
# #################################
167
-
166
+
168
167
if (is.null(ylabs )) {
169
168
if (cumhaz | ! is.null(sfit $ states )) {
170
169
ylabs <- " Cumulative incidence"
171
170
} else {
172
171
ylabs <- " Survival probability"
173
172
}
174
173
}
175
-
176
-
174
+
175
+
177
176
if (length(levels(summary(sfit )$ strata )) == 0 ) {
178
177
# [subs1]
179
178
if (is.null(ystratalabs )) ystratalabs <- as.character(sub(" group=*" , " " , " All" ))
180
179
} else {
181
180
# [subs1]
182
181
if (is.null(ystratalabs )) ystratalabs <- as.character(sub(" group=*" , " " , names(sfit $ strata )))
183
182
}
184
-
183
+
185
184
if (is.null(ystrataname )) ystrataname <- " Strata"
186
185
m <- max(nchar(ystratalabs ))
187
186
times <- seq(0 , max(sfit $ time ), by = timeby )
188
-
187
+
189
188
if (length(levels(summary(sfit )$ strata )) == 0 ) {
190
189
Factor <- factor (rep(" All" , length(subs2 )))
191
190
} else {
192
191
Factor <- factor (summary(sfit , censored = T )$ strata [subs2 ], levels = names(sfit $ strata ))
193
192
}
194
-
193
+
195
194
# Data to be used in the survival plot
196
-
197
-
195
+
196
+
198
197
if (is.null(sfit $ state )) { # no cmprsk
199
198
df <- data.frame (
200
199
time = sfit $ time [subs2 ],
@@ -326,8 +325,9 @@ jskm <- function(sfit,
326
325
scale_labels <- ggplot2 :: waiver()
327
326
if (surv.scale == " percent" ) scale_labels <- scales :: percent
328
327
329
- p <- ggplot2 :: ggplot(df , aes(x = time , y = surv , colour = strata , linetype = strata ) ) + ggtitle(main )
330
-
328
+ p <- ggplot2 :: ggplot(df , aes(x = time , y = surv , colour = strata , linetype = strata )) +
329
+ ggtitle(main )
330
+
331
331
332
332
linecols2 <- linecols
333
333
if (linecols == " black" ) {
@@ -343,8 +343,8 @@ jskm <- function(sfit,
343
343
axis.title.x = element_text(vjust = 0.7 ),
344
344
panel.grid.minor = element_blank(),
345
345
axis.line = element_line(linewidth = 0.5 , colour = " black" ),
346
- legend.position = legendposition ,
347
- legend.background = element_rect(fill = NULL ),
346
+ legend.position = legendposition ,
347
+ legend.background = element_rect(fill = NULL ),
348
348
legend.key = element_rect(colour = NA ),
349
349
panel.border = element_blank(),
350
350
plot.margin = unit(c(0 , 1 , .5 , ifelse(m < 10 , 1.5 , 2.5 )), " lines" ),
@@ -353,16 +353,17 @@ jskm <- function(sfit,
353
353
) +
354
354
scale_x_continuous(xlabs , breaks = times , limits = xlims ) +
355
355
scale_y_continuous(ylabs , limits = ylims , labels = scale_labels )
356
-
357
- if (! is.null(theme )&& theme == ' jama' ) {
358
- p <- p + theme(
356
+
357
+ if (! is.null(theme ) && theme == " jama" ) {
358
+ p <- p + theme(
359
359
panel.grid.major.x = element_blank()
360
360
)
361
- } else {
361
+ } else {
362
362
p <- p + theme(
363
363
panel.grid.major = element_blank()
364
- )}
365
-
364
+ )
365
+ }
366
+
366
367
367
368
# Removes the legend:
368
369
if (legend == FALSE ) {
@@ -372,18 +373,17 @@ jskm <- function(sfit,
372
373
# Add lines too plot
373
374
if (is.null(cut.landmark )) {
374
375
p <- p + geom_step(linewidth = linewidth ) +
375
- scale_linetype_manual(name = ystrataname , values = linetype )
376
+ scale_linetype_manual(name = ystrataname , values = linetype )
376
377
} else {
377
378
p <- p +
378
379
scale_linetype_manual(name = ystrataname , values = linetype ) +
379
- geom_step(data = subset(df , time > = cut.landmark ), linewidth = linewidth ) + geom_step(data = subset(df , time < cut.landmark ), linewidth = linewidth )
380
+ geom_step(data = subset(df , time > = cut.landmark ), linewidth = linewidth ) + geom_step(data = subset(df , time < cut.landmark ), linewidth = linewidth )
380
381
}
381
-
382
- if (! is.null(theme )&& theme == ' jama' ){
383
- p <- p + scale_color_manual(name = ystrataname , values = c(" #00AFBB" , " #E7B800" , " #FC4E07" ))
384
- }else {
385
- p <- p + scale_colour_brewer(name = ystrataname , palette = linecols )
386
-
382
+
383
+ if (! is.null(theme ) && theme == " jama" ) {
384
+ p <- p + scale_color_manual(name = ystrataname , values = c(" #00AFBB" , " #E7B800" , " #FC4E07" ))
385
+ } else {
386
+ p <- p + scale_colour_brewer(name = ystrataname , palette = linecols )
387
387
}
388
388
389
389
@@ -404,7 +404,7 @@ jskm <- function(sfit,
404
404
if (! is.null(cut.landmark )) {
405
405
p <- p + geom_vline(xintercept = cut.landmark , lty = 2 )
406
406
}
407
- p1 <- p
407
+ p1 <- p
408
408
if (showpercent == T ) {
409
409
if (is.null(cut.landmark )) {
410
410
y.percent <- summary(sfit , times = xlims [2 ], extend = T )$ surv
@@ -413,11 +413,10 @@ jskm <- function(sfit,
413
413
}
414
414
if (cumhaz == T & is.null(sfit $ states )) y.percent <- 1 - y.percent
415
415
p <- p + annotate(geom = " text" , x = xlims [2 ], y = y.percent , label = paste0(round(100 * y.percent , 1 ), " %" ), color = " black" )
416
- if (! is.null(theme )&& theme == ' nejm' ) {
417
- p1 <- p1 + annotate(geom = " text" , x = xlims [2 ], y = y.percent , label = paste0(round(100 * y.percent , 1 ), " %" ), color = " black" ,size = nejm.infigure.ratiow * 5 )
418
-
416
+ if (! is.null(theme ) && theme == " nejm" ) {
417
+ p1 <- p1 + annotate(geom = " text" , x = xlims [2 ], y = y.percent , label = paste0(round(100 * y.percent , 1 ), " %" ), color = " black" , size = nejm.infigure.ratiow * 5 )
419
418
}
420
- } else {
419
+ } else {
421
420
y.percent1 <- summary(sfit , times = cut.landmark , extend = T )$ surv
422
421
y.percent2 <- summary(sfit2 , times = xlims [2 ], extend = T )$ surv
423
422
if (! is.null(sfit $ states )) {
@@ -430,12 +429,11 @@ jskm <- function(sfit,
430
429
}
431
430
p <- p + annotate(geom = " text" , x = cut.landmark , y = y.percent1 , label = paste0(round(100 * y.percent1 , 1 ), " %" ), color = " black" ) +
432
431
annotate(geom = " text" , x = xlims [2 ], y = y.percent2 , label = paste0(round(100 * y.percent2 , 1 ), " %" ), color = " black" )
433
- if (! is.null(theme )&& theme == ' nejm' ) {
434
- p1 <- p1 + annotate(geom = " text" , x = cut.landmark , y = y.percent1 , label = paste0(round(100 * y.percent1 , 1 ), " %" ), color = " black" ,size = nejm.infigure.ratiow * 5 ) +
435
- annotate(geom = " text" , x = xlims [2 ], y = y.percent2 , label = paste0(round(100 * y.percent2 , 1 ), " %" ), color = " black" ,size = nejm.infigure.ratiow * 5 )
436
-
432
+ if (! is.null(theme ) && theme == " nejm" ) {
433
+ p1 <- p1 + annotate(geom = " text" , x = cut.landmark , y = y.percent1 , label = paste0(round(100 * y.percent1 , 1 ), " %" ), color = " black" , size = nejm.infigure.ratiow * 5 ) +
434
+ annotate(geom = " text" , x = xlims [2 ], y = y.percent2 , label = paste0(round(100 * y.percent2 , 1 ), " %" ), color = " black" , size = nejm.infigure.ratiow * 5 )
437
435
}
438
- }
436
+ }
439
437
}
440
438
441
439
@@ -456,7 +454,7 @@ jskm <- function(sfit,
456
454
457
455
if (length(levels(summary(sfit )$ strata )) == 0 ) pval <- F
458
456
# if(!is.null(cut.landmark)) pval <- F
459
-
457
+
460
458
if (pval == TRUE ) {
461
459
if (is.null(data )) {
462
460
data <- tryCatch(eval(sfit $ call $ data ), error = function (e ) e )
@@ -484,7 +482,7 @@ jskm <- function(sfit,
484
482
485
483
pvaltxt <- ifelse(pvalue < 0.001 , " p < 0.001" , paste(" p =" , round(pvalue , 3 )))
486
484
if (pval.testname ) pvaltxt <- paste0(pvaltxt , " (Log-rank)" )
487
-
485
+
488
486
# MOVE P-VALUE LEGEND HERE BELOW [set x and y]
489
487
if (is.null(pval.coord )) {
490
488
p <- p + annotate(" text" , x = (as.integer(max(sfit $ time ) / 5 )), y = 0.1 + ylims [1 ], label = pvaltxt , size = pval.size )
@@ -520,7 +518,7 @@ jskm <- function(sfit,
520
518
pvaltxt <- ifelse(pvalue < 0.001 , " p < 0.001" , paste(" p =" , round(pvalue , 3 )))
521
519
522
520
if (pval.testname ) pvaltxt <- paste0(pvaltxt , " (Log-rank)" )
523
-
521
+
524
522
if (is.null(pval.coord )) {
525
523
p <- p + annotate(" text" , x = c(as.integer(max(sfit $ time ) / 10 ), as.integer(max(sfit $ time ) / 10 ) + cut.landmark ), y = 0.1 + ylims [1 ], label = pvaltxt , size = pval.size )
526
524
} else {
@@ -576,22 +574,22 @@ jskm <- function(sfit,
576
574
# ######################
577
575
# Plotting the graphs #
578
576
# ######################
579
-
580
- if (! is.null(theme )&& theme == ' nejm' ) {
581
- p2 <- p1 + coord_cartesian(ylim = nejm.infigure.ylim )+ theme(legend.position = ' none' ,axis.title.x = element_blank(),axis.title.y = element_blank(),
582
- axis.text = element_text(size = 10 * nejm.infigure.ratiow ))
583
- p <- p + patchwork :: inset_element(p2 , 1 - nejm.infigure.ratiow ,1 - nejm.infigure.ratioh , 1 , 1 ,align_to = ' panel' )
577
+
578
+ if (! is.null(theme ) && theme == " nejm" ) {
579
+ p2 <- p1 + coord_cartesian(ylim = nejm.infigure.ylim ) + theme(
580
+ legend.position = " none" , axis.title.x = element_blank(), axis.title.y = element_blank(),
581
+ axis.text = element_text(size = 10 * nejm.infigure.ratiow )
582
+ )
583
+ p <- p + patchwork :: inset_element(p2 , 1 - nejm.infigure.ratiow , 1 - nejm.infigure.ratioh , 1 , 1 , align_to = " panel" )
584
584
}
585
-
585
+
586
586
if (table == TRUE ) {
587
587
ggpubr :: ggarrange(p , blank.pic , data.table ,
588
- nrow = 3 ,
589
- # align = "v",
588
+ nrow = 3 ,
589
+ # align = "v",
590
590
heights = c(2 , .1 , .25 )
591
591
)
592
592
} else {
593
- p
593
+ p
594
594
}
595
595
}
596
-
597
-
0 commit comments