1

Using the eha and survival packages in R, I am using the following code to create the survival plot below:

with(dataset, plot(Surv(enter, exit, event), ylim= c(0.87, 1.0), fn = "surv", strata = Gender))

enter image description here

Rather than exporting the image of the plot, how can I export the X and Y values for each line on this plot? I attempted to use the following code, however it only creates a blank .csv file:

write.table((with(dataset, plot(Surv(enter, exit, event), ylim= c(0.87, 1.0), fn = "surv", strata = Gender))), file="testfit.csv", sep=',')

Thanks for any help in advance.

On a secondary note, does anyone happen to know how to make this plot look nicer in ggplot2?

Sidney Carton
  • 175
  • 1
  • 1
  • 11
  • 2
    Why would you plot an object and then try to pull the values from the plot??? Pull the values from the object itself! `my_surv = with(dataset, Surv(enter, exit, event))`. Then see `?survfit` or `?plot.survfit` – Gregor Thomas Oct 05 '15 at 23:49
  • 1
    `ggfortify` has nice survival plots http://rpubs.com/sinhrks/plot_surv – hrbrmstr Oct 05 '15 at 23:53
  • I don't follow you, @Gregor. In both ?survfit and ?plot.survift, I don't see options for pulling the values from the object itself. – Sidney Carton Oct 06 '15 at 17:25
  • They're in there all the same. Looking a little more closely, I actually think the `summary.survfit` object is what's easiest to get them out of. Using the example from `?summary.survfit`, `x = summary( survfit( Surv(futime, fustat)~1, data=ovarian)); head(x)`, you'll see `x` has both `time` and `surv` components. – Gregor Thomas Oct 06 '15 at 18:18
  • 1
    @Gregor, thank you. Yes, `summary(survfit(Surv(enter, exit, event) ~ 1, data=dataset))` outputs what I need. My next challenge is exporting to a .csv. `write.table(summary(survfit(Surv(enter, exit, event) ~ 1, data=dataset)), file="testfit.csv", sep=',')` throws "cannot coerce class ""summary.survfit"" to a data.frame", and when I try to stratify with `summary(survfit(Surv(enter, exit, event) ~ 1, subset=(Gender==M), data=dataset))`, RStudio returns "Error in eval(expr, envir, enclos) : object 'M' not found" – Sidney Carton Oct 06 '15 at 23:20
  • Assign the summary and use the components of the summary to build the data frame you want. `x = summary(...); str(x)`. – Gregor Thomas Oct 06 '15 at 23:37
  • See, e.g., [Obtaining Survival Estimates in R](http://stackoverflow.com/a/9537026/903061). – Gregor Thomas Oct 06 '15 at 23:39
  • Appreciate the help, @Gregor. I wish I understood why `as.data.frame(summary(survfit(Surv(enter, exit, event) ~ 1, data=dataset)))` again throws "cannot coerce class ""summary.survfit"" to a data.frame". – Sidney Carton Oct 08 '15 at 18:32
  • `x = summary(survfit(Surv(enter, exit, event) ~ 1, data=dataset)); str(x)` produces the below results. Do these indicate how to coerce class to a data.frame? `List of 15 $ n: int $ time: num $ n.risk: num $ n.event: num $ n.censor: num $ n.enter: num $ surv: num $ type: chr "counting" $ std.err: num $ upper: num $ lower: num $ conf.type: chr "log" $ conf.int : num 0.95 $ call: language survfit(formula = Surv(enter, exit, event) ~ 1, data = dataset) $ table: Named num ..- attr(*, "names")= chr "records" "n.max" "n.start" "events" ... - attr(*, "class")= chr "summary.survfit"` – Sidney Carton Oct 08 '15 at 18:34
  • @Gregor, I've done as you suggested and assigned the summary `fit <- survfit(Surv(enter, exit, event) ~ 1, data=dataset)` and as in [Obtaining...](http://stackoverflow.com/questions/9535668/obtaining-survival-estimates-in-r/9537026#9537026) I've tried to coerce to data frame with `as.data.frame(summary(fit)[c("time", "survival")])`. The error being thrown now is _Error in data.frame(time = c(.1, .2, .5, .6, .8, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, : arguments imply differing number of rows: 74, 0_. Now what should I do? – Sidney Carton Oct 19 '15 at 20:49
  • Maybe make a [reproducible example](http://stackoverflow.com/q/5963269/903061)? Can you share some data or find built-in data with similar structure that generates the same error? – Gregor Thomas Oct 19 '15 at 22:48

1 Answers1

1

You can use the gridGraphics package to convert your plot to a grob, then grab the points from out of the grob object. I like this solution because it is applicable to a wide range of problems.

Using the graph that was created here for an example graph:

# plot a graph
library(survival)
library(grid)
library(gridGraphics)
data(lung)
lung.surv <- survfit(Surv(time,status) ~ 1, data = lung)
plot(lung.surv)

# capture the plotted output as a grob
grid.echo()
grid.grab() -> k

# pull out the data from the grob..
k$children$`graphics-plot-1-points-1`$x -> x
k$children$`graphics-plot-1-points-1`$y -> y

I noticed that your plot is stratified by gender...Make sure you pull out the correct subset of the points for each gender.

Chris
  • 1,575
  • 13
  • 20
  • Thanks, @Chris. This modified code still generates my survival curve per your template above: data.surv <- survfit(Surv(enter, exit, event) ~ 1, data = dataset) And now that I've entered the rest of your code, how do I access/export the X and Y values? Also I'm not sure how to stratify by gender. I've tried this code below, but the plot ends up with just a horizontal line and then a vertical line instead of a normal survival curve: data.surv <- survfit(Surv(enter, exit, event) ~ 1, subset=(Gender), data = dataset) How should I be subsetting? – Sidney Carton Oct 06 '15 at 20:04
  • What dataset are you using and what is the purpose of the analysis? If you're trying to plot a new survival curve with the x and y values, I would recommend using ggfortify – Chris Oct 06 '15 at 20:52