Survival analysis in R; creating interactive kaplan-meier plot in with plotly package
One option is to extract the ggplot figure from a ggsurvplot
object.
library(survminer)
p <- ggsurvplot(km_trt_fit)
ggplotly(p[[1]])
To obtain table data on events at different time points, you can work with p[[3]]
.
Error adding new curve to survival curve in ggplot?
If you specify inherit.aes = FALSE
in geom_ribbon()
you avoid that specific error, i.e.
library(survival)
#install.packages("ggfortify")
library(ggfortify)
#> Warning: package 'ggfortify' was built under R version 4.1.2
#> Loading required package: ggplot2
data(aml, package = "survival")
#> Warning in data(aml, package = "survival"): data set 'aml' not found
# Fit the Kaplan-Meier curve
fit <- survfit(Surv(time, status) ~ x, data=aml)
# Create an additional dataset to plot on top of the Kaplan-Meier curve
df <- data.frame(x = seq(1, 150, length.out=10),
y = seq(0, 1, length.out=10),
ymin = seq(0, 1, length.out=10) - 0.1,
ymax = seq(0, 1, length.out=10) + 0.1)
autoplot(fit, conf.int = FALSE, censor = FALSE) +
geom_line(data = df, mapping = aes(x=x, y=y)) +
geom_line(data = df, mapping = aes(x=x, y=ymin)) +
geom_line(data = df, mapping = aes(x=x, y=ymax))
autoplot(fit, conf.int = FALSE, censor = FALSE) +
geom_ribbon(data = df, mapping = aes(x=x, ymin=ymin, ymax=ymax),
inherit.aes = FALSE)
Created on 2022-02-21 by the reprex package (v2.0.1)
Does that solve your problem?
Reconstruct survival curve from coordinates
Use the ggsurvplot
function of the package survminer
which is built on top of ggplot2
and can be used to create Kaplan-Meier plots.
library(survminer)
km_data %>% ggsurvplot()
Update 1
Update for new data
km_data <- data.table(time = c(72, 109, 143),
n.risk = c(80, 77, 76),
n.event = c(1, 1, 2),
n.censor = c(3, 2, 0),
surv = c(0.9875, 0.974675324675325, 0.949025974025974),
strata = c(0, 0, 0),
type = "right")
library(survminer)
km_data %>% ggsurvplot()
R smooth survival curve force start and end
Based on IRTFM comment I was able to find the answer, here is the code:
library(cobs)
library(ggplot2)
df1<-data.frame(y=c(1,0.99,0.97,0.95,0.94,0.94,0.82,0.72,0.58,0.34,0.20,0.12,0.11,0),
time=c(3,4,7,8,10,11,13,14,15,17,20,22,23,24))
con2 <- rbind(c( 1,min(df1$time),1),
c(-1,max(df1$time),0))
Sb1 <- cobs(df1$time,df1$y, constraint="decrease", nknots=4,pointwise= con2,
degree = 2)
summary(Sb1)
plot(Sb1, main='Survival Curve')
Thanks
How do I plot a curve on top of a ggplotsurv Kaplan Meier
the problem is in the use of geom_line
. Since you haven't specified the data
argument inside geom_line
it is NULL
. In the help of geom_line
it is stated that the data is inherited from the plot data if data = NULL
inside geom_line
.
You can specify the data
argument inside geom_line
to get rid of the problem.
Attached there is a reproducible example. I took the numbers given in your question, but I've adjusted the vector meansurv
so that this vector has more entries than the number of rows of the data frame treatment
.
First I have reproduced your error and then I've shown how one can do it right using the argument data
of the function geom_line()
library(survival)
library(survminer)
#> Lade nötiges Paket: ggplot2
#> Lade nötiges Paket: ggpubr
# a preview of the data frame.
treatment <- data.frame(
treatment = c(1, 1, 1, 1, 1, 1),
t = c(5.525, 1.9493, 4.9473, 5.9466, 1.5797, 0.5038),
event = c(1, 1, 1, 0, 1, 1)
)
# modified vector:
meansurv <- c(1.0000000, 0.9129731, 0.8337045, 0.7614860, 0.6956758, 0.6356917, 0.50, 0.43, 0.37)
f <- survfit(Surv(t, event)~1, data=treatment)
f1 <- ggsurvplot(f); f1
# data is not used in geom_line ->> raises error:
f2 <- f1$plot + geom_line(aes(seq.int(meansurv) - 1, meansurv)); f2
#> Error: Aesthetics must be either length 1 or the same as the data (7): x and y
# using argument data:
f2 <- f1$plot + geom_line(data = data.frame(x = seq.int(meansurv) - 1,
y = meansurv),
aes(x = x, y = y)); f2
Created on 2020-07-21 by the reprex package (v0.3.0)
Add overall survival curve to survival curve plot for a covariate
One way is to construct all the objects you need by hand. This gives you great power. But remember, with great power comes great responsibility. Here's what I did. I merged fitted values for average model with the model where survival is modelled against rx
. I fiddled with the colors a bit to make the average model black. Feel free to play around with colors, line types, sizes...
library (survival)
library(GGally)
library(ggplot2)
data(colon)
# This generates the overall survival curve (without covariates):
kms_avg <- survfit(Surv(time, status)~1, data =colon)
g_avg <- ggsurv(kms_avg, surv.col="red", xlab="Time (days)", lty.ci=0)
# This generates the survival curve for covariate 'rx'
kms_rx <- survfit(Surv(time, status)~rx, data =colon)
g_rx <- ggsurv(kms_rx, surv.col="red", xlab="Time (days)", lty.ci=0)
s_avg <- summary(kms_avg)
s_rx <- summary(kms_rx)
s <- data.frame(time = s_avg$time, surv = s_avg$surv, strata = "average")
s_rx <- data.frame(time = s_rx$time, surv = s_rx$surv, strata = s_rx$strata)
xy <- rbind(s, s_rx)
ggplot(xy, aes(x = time, y = surv, color = strata)) +
theme_bw() +
geom_point() +
geom_line()
ggplot(xy, aes(x = time, y = surv, color = strata)) +
theme_bw() +
geom_point(size = 0.5) +
geom_line() +
scale_color_manual(values = c("black", "#e41a1c", "#377eb8", "#4daf4a"))
Or, you could manually define grouping.
g_rx + geom_point(data=s, aes(group = 1), color="black")
Related Topics
Produce an Inset in Each Facet of an R Ggplot While Preserving Colours of the Original Facet Content
Fill Area Between Two Lines, with High/Low and Dates
R/Quantmod: Multiple Charts All Using the Same Y-Axis
Extract Column Name in Mutate_If Call
Arrange_() Multiple Columns with Descending Order
Why Are Lubridate Functions So Slow When Compared with As.Posixct
Using 'Fread' to Import CSV File from an Archive into 'R' Without Extracting to Disk
How Would You Fit a Gamma Distribution to a Data in R
Colons Equals Operator in R? New Syntax
Ggplot Object Not Found Error When Adding Layer with Different Data
How Do Add a Column in a Data Frame in R
Convert Begin and End Coordinates into Spatial Lines in R
Creating a Sankey Diagram Using Networkd3 Package in R