Survival Plots with Survminer - Blank Plot
Asked Answered
J

2

10

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'
)
Judo answered 27/2, 2017 at 6:25 Comment(3)
Please show a MCVE : stackoverflow.com/help/how-to-ask, https://mcmap.net/q/40507/-how-to-make-a-great-r-reproducible-example How did you create plot_anylocaltreatment and plot_aggressive?Basutoland
My apologies - I'm still fairly new. I've attached a reproducible example in the edit above.Judo
Michael, try ditching the notebook and render as HTML. Addresses the issue for me.Shirker
C
4

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)
Cistercian answered 20/7, 2017 at 15:29 Comment(3)
This doesn't work if you have the table at the bottom. And if you do p$plot, you don't even need print()Shirker
I guess what @Shirker means is the 'at risk table'. This is indeed not shown. You could also call p$table but then the 'at risk table' is nearly the same size as the plot.Etherize
to also print the "at risk table" you can call p$table in a separate R chunk and resize it in the chunk options, e.g.: {r, fig.height = 4, fig.width = 5.5, position=center} p$plot and {r, fig.height = 1, fig.width = 5.5, position=center} p$table this way you will omit the blank page and can size plot and table separately. Dirty workaround though.Bloomy
B
3

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.

Bilabial answered 19/7, 2020 at 22:18 Comment(1)
Still works well in 2023. Thanks!Fiesole

© 2022 - 2024 — McMap. All rights reserved.