Michael Luu
Michael Luu

Reputation: 609

Survival Plots with Survminer - Blank Plot

I'm currently having an issue with blank plots appearing in rmarkdown chunk outputs for survminer. Please see image below.

enter image description here

It makes output difficult as it includes a huge empty space while trying to author reports.

enter image description here

I've been investigating this issue and I've narrowed it down to having to do with this print 'newpage' argument -

enter image description here

My question is - can anybody explain what exactly is happening here? - why is there a 'blank' plot and how can I not have it show ? - what exactly is happening when I have newpage = F for the first plot and newpage = T for the second plot to not have the blank page show ? - is there any other method of NOT having the first blank plot show ?

Thank you!

EDIT:

Reproducible Example -

require(survminer)
require(survival)

Data <- data.frame(
X = sample(1:30),
Y = sample(c(1,0), 30, replace = TRUE),
Z = sample(c(1,0), 30, replace = TRUE)
)

ggsurvplot(
 survfit(Surv(Data$X, Data$Y) ~ Data$Z),
 risk.table = T,
 break.time.by = 12,
 risk.table.fontsize = 3,
 font.tickslab = 10,
 font.x = 11,
 xlab = 'Time (Months)',
 font.y = 11,
 font.main = 11,
 legend = c(0.8, .9),
 legend.title = '',
 risk.table.height = .20,
 risk.table.title = element_blank(),
 censor = F,
 pval = T,
 pval.coord = c(6, .00),
 pval.size = 4,
 surv.scale = 'percent',
 risk.table.y.text = F,
 palette = 'Set1'
)

Upvotes: 10

Views: 2263

Answers (2)

Mitch
Mitch

Reputation: 31

This seems to work for me :

plot.new() 
print(p,newpage = FALSE)

This will also work if the plot has the risk table. This solution is better than doing first plot(p$plot) and then plot(p$table) as suggested by others, because the table and plots remain nicely aligned.

Upvotes: 3

jdb
jdb

Reputation: 147

In order to print the survival plot with no leading blank plots, print just the plot object returned by ggsurvplot(). For example,

library(survival)
library(survminer)
fit <- survfit(Surv(time, status) ~ sex, data = lung)
p <- ggsurvplot(fit, data = lung)
print(p$plot)

Upvotes: 4

Related Questions