Skip to content

Commit

Permalink
Merge pull request #10 from parmsam-pfizer/4-update-km-plot-code
Browse files Browse the repository at this point in the history
Switch over to ggsurvfit for KM plot
  • Loading branch information
rpodcast committed May 3, 2024
2 parents b5df29c + 9901d9e commit cbd578a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 15 deletions.
39 changes: 24 additions & 15 deletions app/views/km_plot.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
box::use(
dplyr[filter, inner_join, mutate, select],
ggplot2[element_text, geom_hline, rel, theme, theme_bw, theme_set],
ggplot2[element_text, geom_hline, rel, theme, theme_bw, theme_set, labs],
shiny[
NS, observeEvent, p, plotOutput, reactiveValues, renderPlot, renderUI, selectInput,
tagAppendAttributes, tagList, tags, uiOutput, updateSelectInput,
fluidRow, column
],
visR[add_CI, add_CNSR, estimate_KM, visr]
ggsurvfit[
Surv, survfit2, ggsurvfit, add_confidence_interval, add_risktable,
add_censor_mark, scale_ggsurvfit
],
scales[breaks_width]
)

box::use(
Expand Down Expand Up @@ -107,17 +111,20 @@ server <- function(input, output, session, datasets) {
AVAL = AVAL / 30.4167
)

surv_mod <- estimate_KM(data = anl, strata = "TRT01A")
surv_mod <- survfit2(Surv(AVAL, 1 - CNSR) ~ TRT01A, data = anl)
theme_set(theme_bw())

km_plot <- visr(surv_mod,
y_label = "Survival Probability (%)",
x_label = "Time (Months)",
fun = "pct",
legend_position = "bottom"
) |>
add_CNSR() |>
add_CI()
km_plot <- ggsurvfit(surv_mod) +
add_censor_mark() +
add_confidence_interval() +
add_risktable(
risktable_stats = "n.risk",
size = rel(5)
) +
labs(
y = "Survival Probability",
x = "Time (Months)",
)

km_plot <- km_plot +
theme(
Expand All @@ -126,10 +133,12 @@ server <- function(input, output, session, datasets) {
legend.text = element_text(size = rel(1.3)),
legend.title = element_text(size = rel(1.4))
) +
geom_hline(yintercept = 0.5, linetype = "dashed")

km_plot <- km_plot |>
add_risktable2(group = "statlist")
scale_ggsurvfit(
x_scales = list(
breaks = scales::breaks_width(0.5)
)
) +
geom_hline(yintercept = 0.05, linetype = "dashed")

output$plot_title <- renderUI({
tags$div(
Expand Down
38 changes: 38 additions & 0 deletions renv.lock
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,26 @@
],
"Hash": "3a147ee02e85a8941aad9909f1b43b7b"
},
"ggsurvfit": {
"Package": "ggsurvfit",
"Version": "1.0.0",
"Source": "Repository",
"Repository": "RSPM",
"Requirements": [
"R",
"broom",
"cli",
"dplyr",
"ggplot2",
"glue",
"gtable",
"patchwork",
"rlang",
"survival",
"tidyr"
],
"Hash": "42d3b3206360e4fb840a7c9311b975cc"
},
"glue": {
"Package": "glue",
"Version": "1.6.2",
Expand Down Expand Up @@ -1233,6 +1253,24 @@
],
"Hash": "2a0dc8c6adfb6f032e4d4af82d258ab5"
},
"patchwork": {
"Package": "patchwork",
"Version": "1.2.0",
"Source": "Repository",
"Repository": "RSPM",
"Requirements": [
"cli",
"ggplot2",
"grDevices",
"graphics",
"grid",
"gtable",
"rlang",
"stats",
"utils"
],
"Hash": "9c8ab14c00ac07e9e04d1664c0b74486"
},
"pillar": {
"Package": "pillar",
"Version": "1.9.0",
Expand Down

0 comments on commit cbd578a

Please sign in to comment.