Skip to content

Commit e52a188

Browse files
Apply automatic stylistic changes
1 parent dcd22d6 commit e52a188

File tree

4 files changed

+97
-97
lines changed

4 files changed

+97
-97
lines changed

R/jskm.R

+56-58
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,10 @@ jskm <- function(sfit,
115115
showpercent = F,
116116
status.cmprsk = NULL,
117117
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),
123122
...) {
124123
#################################
125124
# sorting the use of subsetting #
@@ -128,7 +127,7 @@ jskm <- function(sfit,
128127
n.risk <- n.censor <- surv <- strata <- lower <- upper <- NULL
129128

130129
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"
132131
if (is.null(subs)) {
133132
if (length(levels(summary(sfit)$strata)) == 0) {
134133
subs1 <- 1
@@ -158,43 +157,43 @@ jskm <- function(sfit,
158157
subs2 <- which(regexpr(ssvar, summary(sfit, censored = T)$strata, perl = T) != -1)
159158
subs3 <- which(regexpr(ssvar, summary(sfit, times = times, extend = TRUE)$strata, perl = T) != -1)
160159
}
161-
160+
162161
if (!is.null(subs) | !is.null(sfit$states)) pval <- FALSE
163-
162+
164163
##################################
165164
# data manipulation pre-plotting #
166165
##################################
167-
166+
168167
if (is.null(ylabs)) {
169168
if (cumhaz | !is.null(sfit$states)) {
170169
ylabs <- "Cumulative incidence"
171170
} else {
172171
ylabs <- "Survival probability"
173172
}
174173
}
175-
176-
174+
175+
177176
if (length(levels(summary(sfit)$strata)) == 0) {
178177
# [subs1]
179178
if (is.null(ystratalabs)) ystratalabs <- as.character(sub("group=*", "", "All"))
180179
} else {
181180
# [subs1]
182181
if (is.null(ystratalabs)) ystratalabs <- as.character(sub("group=*", "", names(sfit$strata)))
183182
}
184-
183+
185184
if (is.null(ystrataname)) ystrataname <- "Strata"
186185
m <- max(nchar(ystratalabs))
187186
times <- seq(0, max(sfit$time), by = timeby)
188-
187+
189188
if (length(levels(summary(sfit)$strata)) == 0) {
190189
Factor <- factor(rep("All", length(subs2)))
191190
} else {
192191
Factor <- factor(summary(sfit, censored = T)$strata[subs2], levels = names(sfit$strata))
193192
}
194-
193+
195194
# Data to be used in the survival plot
196-
197-
195+
196+
198197
if (is.null(sfit$state)) { # no cmprsk
199198
df <- data.frame(
200199
time = sfit$time[subs2],
@@ -326,8 +325,9 @@ jskm <- function(sfit,
326325
scale_labels <- ggplot2::waiver()
327326
if (surv.scale == "percent") scale_labels <- scales::percent
328327

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+
331331

332332
linecols2 <- linecols
333333
if (linecols == "black") {
@@ -343,8 +343,8 @@ jskm <- function(sfit,
343343
axis.title.x = element_text(vjust = 0.7),
344344
panel.grid.minor = element_blank(),
345345
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),
348348
legend.key = element_rect(colour = NA),
349349
panel.border = element_blank(),
350350
plot.margin = unit(c(0, 1, .5, ifelse(m < 10, 1.5, 2.5)), "lines"),
@@ -353,16 +353,17 @@ jskm <- function(sfit,
353353
) +
354354
scale_x_continuous(xlabs, breaks = times, limits = xlims) +
355355
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(
359359
panel.grid.major.x = element_blank()
360360
)
361-
} else{
361+
} else {
362362
p <- p + theme(
363363
panel.grid.major = element_blank()
364-
)}
365-
364+
)
365+
}
366+
366367

367368
# Removes the legend:
368369
if (legend == FALSE) {
@@ -372,18 +373,17 @@ jskm <- function(sfit,
372373
# Add lines too plot
373374
if (is.null(cut.landmark)) {
374375
p <- p + geom_step(linewidth = linewidth) +
375-
scale_linetype_manual(name = ystrataname, values = linetype)
376+
scale_linetype_manual(name = ystrataname, values = linetype)
376377
} else {
377378
p <- p +
378379
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)
380381
}
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)
387387
}
388388

389389

@@ -404,7 +404,7 @@ jskm <- function(sfit,
404404
if (!is.null(cut.landmark)) {
405405
p <- p + geom_vline(xintercept = cut.landmark, lty = 2)
406406
}
407-
p1<-p
407+
p1 <- p
408408
if (showpercent == T) {
409409
if (is.null(cut.landmark)) {
410410
y.percent <- summary(sfit, times = xlims[2], extend = T)$surv
@@ -413,11 +413,10 @@ jskm <- function(sfit,
413413
}
414414
if (cumhaz == T & is.null(sfit$states)) y.percent <- 1 - y.percent
415415
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)
419418
}
420-
} else {
419+
} else {
421420
y.percent1 <- summary(sfit, times = cut.landmark, extend = T)$surv
422421
y.percent2 <- summary(sfit2, times = xlims[2], extend = T)$surv
423422
if (!is.null(sfit$states)) {
@@ -430,12 +429,11 @@ jskm <- function(sfit,
430429
}
431430
p <- p + annotate(geom = "text", x = cut.landmark, y = y.percent1, label = paste0(round(100 * y.percent1, 1), "%"), color = "black") +
432431
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)
437435
}
438-
}
436+
}
439437
}
440438

441439

@@ -456,7 +454,7 @@ jskm <- function(sfit,
456454

457455
if (length(levels(summary(sfit)$strata)) == 0) pval <- F
458456
# if(!is.null(cut.landmark)) pval <- F
459-
457+
460458
if (pval == TRUE) {
461459
if (is.null(data)) {
462460
data <- tryCatch(eval(sfit$call$data), error = function(e) e)
@@ -484,7 +482,7 @@ jskm <- function(sfit,
484482

485483
pvaltxt <- ifelse(pvalue < 0.001, "p < 0.001", paste("p =", round(pvalue, 3)))
486484
if (pval.testname) pvaltxt <- paste0(pvaltxt, " (Log-rank)")
487-
485+
488486
# MOVE P-VALUE LEGEND HERE BELOW [set x and y]
489487
if (is.null(pval.coord)) {
490488
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,
520518
pvaltxt <- ifelse(pvalue < 0.001, "p < 0.001", paste("p =", round(pvalue, 3)))
521519

522520
if (pval.testname) pvaltxt <- paste0(pvaltxt, " (Log-rank)")
523-
521+
524522
if (is.null(pval.coord)) {
525523
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)
526524
} else {
@@ -576,22 +574,22 @@ jskm <- function(sfit,
576574
#######################
577575
# Plotting the graphs #
578576
#######################
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")
584584
}
585-
585+
586586
if (table == TRUE) {
587587
ggpubr::ggarrange(p, blank.pic, data.table,
588-
nrow = 3,
589-
#align = "v",
588+
nrow = 3,
589+
# align = "v",
590590
heights = c(2, .1, .25)
591591
)
592592
} else {
593-
p
593+
p
594594
}
595595
}
596-
597-

R/svyjskm.R

+33-31
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
#' @export
5555

5656
svyjskm <- function(sfit,
57-
theme=NULL,
57+
theme = NULL,
5858
xlabs = "Time-to-event",
5959
ylabs = "Survival probability",
6060
xlims = NULL,
@@ -88,7 +88,7 @@ svyjskm <- function(sfit,
8888
...) {
8989
surv <- strata <- lower <- upper <- NULL
9090

91-
if(!is.null(theme)&&theme=='nejm') legendposition<-'right'
91+
if (!is.null(theme) && theme == "nejm") legendposition <- "right"
9292
if (is.null(timeby)) {
9393
if (inherits(sfit, "svykmlist")) {
9494
timeby <- signif(max(sapply(sfit, function(x) {
@@ -291,15 +291,16 @@ svyjskm <- function(sfit,
291291
) +
292292
scale_x_continuous(xlabs, breaks = times, limits = xlims) +
293293
scale_y_continuous(ylabs, limits = ylims, labels = scale_labels)
294-
295-
if(!is.null(theme)&&theme=='jama'){
296-
p<-p+theme(
294+
295+
if (!is.null(theme) && theme == "jama") {
296+
p <- p + theme(
297297
panel.grid.major.x = element_blank()
298298
)
299-
} else{
299+
} else {
300300
p <- p + theme(
301301
panel.grid.major = element_blank()
302-
)}
302+
)
303+
}
303304

304305

305306
# Removes the legend:
@@ -310,18 +311,18 @@ svyjskm <- function(sfit,
310311
# Add lines too plot
311312
if (is.null(cut.landmark)) {
312313
p <- p + geom_step(linewidth = linewidth) +
313-
scale_linetype_manual(name = ystrataname, values = linetype)
314+
scale_linetype_manual(name = ystrataname, values = linetype)
314315
} else {
315316
p <- p + geom_step(data = subset(df, time < cut.landmark), linewidth = linewidth) + geom_step(data = subset(df, time >= cut.landmark), linewidth = linewidth) +
316-
scale_linetype_manual(name = ystrataname, values = linetype)
317+
scale_linetype_manual(name = ystrataname, values = linetype)
317318
}
318319

319-
if(!is.null(theme)&&theme=='jama'){
320-
p<-p+scale_color_manual(name=ystrataname, values = c("#00AFBB", "#E7B800", "#FC4E07"))
321-
}else{
322-
p<-p+ scale_colour_brewer(name = ystrataname, palette = linecols)
320+
if (!is.null(theme) && theme == "jama") {
321+
p <- p + scale_color_manual(name = ystrataname, values = c("#00AFBB", "#E7B800", "#FC4E07"))
322+
} else {
323+
p <- p + scale_colour_brewer(name = ystrataname, palette = linecols)
323324
}
324-
325+
325326
# Add 95% CI to plot
326327
if (ci) {
327328
if (linecols2 == "black") {
@@ -334,30 +335,29 @@ svyjskm <- function(sfit,
334335
if (!is.null(cut.landmark)) {
335336
p <- p + geom_vline(xintercept = cut.landmark, lty = 2)
336337
}
337-
338-
p1<-p
338+
339+
p1 <- p
339340
## p-value
340341
if (inherits(sfit, "svykm")) pval <- FALSE
341342
# if(is.null(design)) pval <- FALSE
342343
if (showpercent == TRUE) {
343344
if (is.null(cut.landmark)) {
344345
y.percent <- df[df$time %in% tapply(df$time, df$strata, max), "surv"]
345346
p <- p + annotate(geom = "text", x = xlims[2], y = y.percent, label = paste0(round(100 * y.percent, 1), "%"), color = "black")
346-
if(!is.null(theme)&&theme == 'nejm') {
347-
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)
347+
if (!is.null(theme) && theme == "nejm") {
348+
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)
348349
}
349-
} else {
350+
} else {
350351
df.cut <- df[df$time < cut.landmark, ]
351352
y.percent1 <- df.cut[df.cut$time %in% tapply(df.cut$time, df.cut$strata, max), "surv"]
352353
y.percent2 <- df[df$time %in% tapply(df$time, df$strata, max), "surv"]
353354
p <- p + annotate(geom = "text", x = cut.landmark, y = y.percent1, label = paste0(round(100 * y.percent1, 1), "%"), color = "black") +
354355
annotate(geom = "text", x = xlims[2], y = y.percent2, label = paste0(round(100 * y.percent2, 1), "%"), color = "black")
355-
if(!is.null(theme)&&theme == 'nejm') {
356-
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) +
357-
annotate(geom = "text", x = xlims[2], y = y.percent2, label = paste0(round(100 * y.percent2, 1), "%"), color = "black",size=nejm.infigure.ratiow*5)
358-
359-
}
356+
if (!is.null(theme) && theme == "nejm") {
357+
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) +
358+
annotate(geom = "text", x = xlims[2], y = y.percent2, label = paste0(round(100 * y.percent2, 1), "%"), color = "black", size = nejm.infigure.ratiow * 5)
360359
}
360+
}
361361
}
362362
if (pval) {
363363
if (is.null(design)) {
@@ -527,16 +527,18 @@ svyjskm <- function(sfit,
527527
#######################
528528
# Plotting the graphs #
529529
#######################
530-
if(!is.null(theme)&&theme == 'nejm') {
531-
p2<-p1+coord_cartesian(ylim=nejm.infigure.ylim)+theme(legend.position='none',axis.title.x = element_blank(),axis.title.y=element_blank()
532-
,axis.text= element_text(size=10*nejm.infigure.ratiow))
533-
p<- p + patchwork::inset_element(p2, 1-nejm.infigure.ratiow,1-nejm.infigure.ratioh, 1, 1,align_to = 'panel')
530+
if (!is.null(theme) && theme == "nejm") {
531+
p2 <- p1 + coord_cartesian(ylim = nejm.infigure.ylim) + theme(
532+
legend.position = "none", axis.title.x = element_blank(), axis.title.y = element_blank(),
533+
axis.text = element_text(size = 10 * nejm.infigure.ratiow)
534+
)
535+
p <- p + patchwork::inset_element(p2, 1 - nejm.infigure.ratiow, 1 - nejm.infigure.ratioh, 1, 1, align_to = "panel")
534536
}
535-
537+
536538
if (table == TRUE) {
537539
ggpubr::ggarrange(p, blank.pic, data.table,
538-
nrow = 3,
539-
#align = "v",
540+
nrow = 3,
541+
# align = "v",
540542
heights = c(2, .1, .25)
541543
)
542544
} else {

0 commit comments

Comments
 (0)