I'm currently having an issue with blank plots appearing in rmarkdown chunk outputs for survminer. Please see image below.
It makes output difficult as it includes a huge empty space while trying to author reports.
I've been investigating this issue and I've narrowed it down to having to do with this print 'newpage' argument -
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'
)
The R package survival fits and plots survival curves using R base graphs. There are also several R packages/functions for drawing survival curves using ggplot2 system: The default graph generated with the R package survival is ugly and it requires programming skills for drawing a nice looking survival curves.
survminer: Survival Analysis and Visualization. The survminer R package provides functions for facilitating survival analysis and visualization. The main functions, in the package, are organized in different categories as follow.
We recently released the survminer verion 0.3, which includes many new features to help in visualizing and sumarizing survival analysis results. In this article, we present a cheatsheet for survminer, created by Przemysław Biecek, and provide an overview of main functions.
arrange_ggsurvplots (): Arranges multiple ggsurvplots on the same page. ggsurvevents (): Plots the distribution of event’s times. surv_summary (): Summary of a survival curve.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With