Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stat_km and geom_km for Kaplan-Meier Survival curves #1080

Closed
sachsmc opened this issue Apr 2, 2015 · 8 comments
Closed

stat_km and geom_km for Kaplan-Meier Survival curves #1080

sachsmc opened this issue Apr 2, 2015 · 8 comments

Comments

@sachsmc
Copy link

sachsmc commented Apr 2, 2015

People occasionally ask about plotting survival curves with ggplot2. While it is possible, see here for some examples, it's not done in the "ggplot way" and people often revert to the base plot because it's so much easier (but not nearly as powerful).

I've worked a bit on adding a stat and geom for computing and plotting survival curves here. I view it as along the same lines as stat_ecdf but for censored data. Before submitting a pull request I wanted to ask if this would be suitable for inclusion in ggplot2, or if it would be better as a standalone package?

Here's an example code and plot:

df <- survival::lung
ggplot(df, aes(x = time, status = status, color = factor(sex))) + geom_km()

image

@MarcinKosinski
Copy link

It looks like autoplot function from package survMisc produces KM curves for survfit object in a ggplot-way. It also produces a table with risk set size dependency over time.
http://www.inside-r.org/packages/cran/survMisc/docs/autoplot

library(survMisc)
data(kidney, package="KMsurv")
s1 <- survfit(Surv(time=time, event=delta) ~ type, data=kidney)
autoplot(s1)
autoplot(s1, CI=TRUE, pval=TRUE, plotTable=TRUE, divideTime=5,
 legendLabs=c("surgical", "percutaneous"),
 title="Time to infection following catheter placement \n
   by type of catheter, for dialysis patients")
s1 <- survfit(Surv(time=time, event=delta) ~ 1, data=kidney)
autoplot(s1)
data(rectum.dat, package="km.ci")
s1 <- survfit(Surv(time, status) ~ 1, data=rectum.dat)
### change confidence intervals to log Equal-Precision confidence bands
km.ci::km.ci(s1, method="logep")
autoplot(s1, bands=TRUE)

@sachsmc
Copy link
Author

sachsmc commented Jun 5, 2015

@MarcinKosinski Thanks for pointing that out, I was not aware of that function. While this autoplot does use ggplot2 to create a survival plot, I don't agree that it is done in the "ggplot way". I.e., there is no support for faceting, scale transformations, or even themes. Furthermore, it ignores the key concepts of the grammar of graphics, such as aesthetic mappings, layers, statistical transformations, and so on.

@hadley
Copy link
Member

hadley commented Jun 10, 2015

I'd rather it go in a separate package - have you considered adding the data related parts to broom?

@hadley hadley closed this as completed Jun 10, 2015
@fpbarthel
Copy link

I'm very much interested in these functions, have been looking for survival functionality for ggplot forever. Found some solutions but none have been very satisfactory. How can I access these functions?

@sachsmc
Copy link
Author

sachsmc commented Feb 18, 2016

@fpbarthel They're available at my repo here, still under development and not on CRAN yet.

@fpbarthel
Copy link

Thanks

@MarcinKosinski
Copy link

Have a look at 'survminer' package :) available at CrAN and github

Marcin Kosinski

Dnia 18.02.2016 o godz. 18:18 fpbarthel notifications@github.com napisał(a):

I'm very much interested in these functions, have been looking for survival functionality for ggplot forever. Found some solutions but none have been very satisfactory. How can I access these functions?


Reply to this email directly or view it on GitHub.

@srw6v
Copy link

srw6v commented Apr 4, 2016

@MarcinKosinski great package. Much more flexible than autoplot.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants