Moniqueee
Moniqueee

Reputation: 41

misalignment between the chart and the risk table in ggsurvplot

I am observing an ugly misalignment between the chart and the risk table plotting with ggsurvplot of survminer package in R version 4.3.3 (2024-02-29 ucrt) -- "Angel Food Cake"

enter image description here

I would like to display (as the code has always done until a couple of days ago) survival chart and risk table perfectly aligned so that the year notches in the two axes coincide.

Upvotes: 4

Views: 1193

Answers (4)

andrewj
andrewj

Reputation: 3057

Here's a solution that uses the library patchwork and also see the post where this is discussed, in the context of labels for the risk table.

The following code demonstrates the misalignment in the KM figure and the risk table (and also a bug where the number at risk at time 0 is not being shown):

library(survival)
library(survminer)

the.df <- data.frame(group=c('a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'd'), 
                     time=c(1,2,3,4,5,6,1,2,3,4,5,6), 
                     status = c(1,1,0,1,0,1,0,1,0,1,1,0))

result <- survfit(Surv(the.df$time, the.df$status==1) ~ group, data=the.df)

ggsurvplot(result,
           risk.table=T)

enter image description here

The patchwork package and break.time.by solve this problem:

library(survival)
library(survminer)
library(patchwork)

the.df <- data.frame(group=c('a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'd'), 
                 time=c(1,2,3,4,5,6,1,2,3,4,5,6), 
                 status = c(1,1,0,1,0,1,0,1,0,1,1,0))

result <- survfit(Surv(the.df$time, the.df$status==1) ~ group, data=the.df)

p1 <- ggsurvplot(result, 
           risk.table=T,
           break.time.by=1)

p1$plot
p1$plot/p1$table + plot_layout(heights=c(8,2))

enter image description here

Now everything is lined up and the number at risk for time 0 are shown.

Upvotes: 1

Jessica Grindheim
Jessica Grindheim

Reputation: 51

Did you find a solution? I hate it when your code breaks after an update.

Also grabbing the legend from ggsurvplot broke too. But I figured out a work-around.

# get legend from ggsurvplot (doesn't work after updates)
legend <- cowplot::get_legend( toReturn[[ paste0("4a.KM") ]][[ "plot" ]] )

# get legend from ggsurvplot (work-around after update)

# 1. Get component names
plot_component_names(  KM[[ "plot" ]] )  # get component names for get_plot_component

# 2. Get legend
legend = cowplot::get_plot_component( KM[[ "plot" ]], 'guide-box-inside', return_all = TRUE)

Upvotes: 0

Jessica Grindheim
Jessica Grindheim

Reputation: 51

I figured it out! Revert to old ggplot until the issue is fixed.

url <- "https://cran.r-project.org/src/contrib/Archive/ggplot2/ggplot2_3.4.4.tar.gz"

install.packages(url, type="source", repos=NULL)  

Upvotes: 5

Denzo
Denzo

Reputation: 355

This may have to do with updates in the scales, ggplot2 and/or cowplot packages. I would recommend updating all R packages you got to the newest version and trying this again.

If this doesn't help, you may want to check out the adjustedCurves package, which offers similar functionality.

Upvotes: 0

Related Questions