How to center align a KM plot title in R?

I am using the following sample data and code to draw a KM plot, but can't seem to center align the title using my code. The resulting graph which is saved using my code seems to miss this alignment code. Any suggestions to help ensure that the code used to save the plot is able to recognize my preceding code properly to result in a center aligned title?

structure(list(X = 1:26, OS = c(59, 115, 156, 421, 431, 448,  464,
475, 477, 563, 638, 744, 769, 770, 803, 855, 1040, 1106,  1129, 1206,
1227, 268, 329, 353, 365, 377), OS_event = c(1, 1,  1, 0, 1, 0, 1, 1,
0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,  1, 1, 0), age = c("> 50
years", "> 50 years", "> 50 years", "> 50 years",  "> 50 years", "> 50
years", "> 50 years", "> 50 years", "> 50 years",  "> 50 years", "> 50
years", "> 50 years", "> 50 years", "< 50 years",  "< 50 years", "< 50
years", "< 50 years", "< 50 years", "< 50 years",  "< 50 years", "< 50
years", "< 50 years", "< 50 years", "< 50 years",  "< 50 years", "< 50
years"), resid.ds = c(2L, 2L, 2L, 2L, 2L,  1L, 2L, 2L, 2L, 1L, 1L, 1L,
2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L,  2L, 2L, 1L, 2L, 1L), rx = c(1L,
1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L,  2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 1L, 1L, 2L, 2L,  2L), ecog.ps = c(1L, 1L, 2L, 1L, 1L, 2L,
2L, 2L, 1L, 2L, 2L,  1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L,
2L, 1L, 1L)), row.names = c(NA, 
-26L), class = "data.frame")
Load data and packages
pacman::p_load(pacman, party, rio, tidyverse) 
library(survival) 
library(survminer) 
library(ggtext)

ovarian <- read.csv("~/Desktop/ovarian.csv", stringsAsFactors = FALSE)
 

ovarian$OS = as.numeric(ovarian$OS) ovarian$OS_event =
as.numeric(ovarian$OS_event)

# Run KM plot: 

custom_theme <- function() {
  theme_survminer() %+replace%
    theme(
      plot.title=element_text(hjust=0.5, size=16, margin = margin(c(0,0,15,0), unit = "pt"))
    )
}

p <- ggsurvplot(survfit(Surv(OS, OS_event) ~ ovarian$age,
                    data = ovarian), 
            risk.table = TRUE,
            pval = TRUE,
            pval.coord = c(72,0.8),
            legend.labs = c("< 55 years", "> 55 years"), 
            title = "OS by binarised median age on
           supplement Tx preceding disease diagnosis",
            ggtheme=custom_theme(),
            legend = "right",
            legend.title = "Binarised median age", 
            break.x.by=24,
            palette = c("red", "blue"), 
            xlab="Time (Months)",
            xlim=c(0,96))

ggsave(filename = "Test.png",
       plot = print(p),
       path = "~/Desktop/",
       width = 7,
       height = 7,
       dpi =  300)

Hi
just add \n between those two lines of the title (before the word supplement), as given below
title = "OS by binarised median age on\nsupplement Tx preceding disease diagnosis",


Hope it helps

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.