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

Capture the risk table as a data frame #158

Closed
kassambara opened this issue Feb 28, 2017 · 1 comment
Closed

Capture the risk table as a data frame #158

kassambara opened this issue Feb 28, 2017 · 1 comment

Comments

@kassambara
Copy link
Owner

@kassambara kassambara commented Feb 28, 2017

(e-mail from a user)

I would like to capture the at risk table as a data frame, looking more or less like what is printed at the bottom of the graph. In the returned list from ggplotsurv the table portion of the list is actually a graph; that isn't want I want. Is there a way to capture the table itself.

kassambara added a commit that referenced this issue Feb 28, 2017
@kassambara
Copy link
Owner Author

@kassambara kassambara commented Feb 28, 2017

Now, ggsurvplot() returns an object of class ggsurvplot which is list containing the following components:

  • plot: the survival plot (ggplot object)
  • table: the number of subjects at risk table per time (ggplot object).
  • cumevents: the cumulative number of events table (ggplot object)
  • ncensor.plot: the number of censoring. (ggplot object)
  • data.survplot: the data used to plot the survival curves (data.frame).
  • data.survtable: the data used to plot the tables under the main survival curves (data.frame).

Fit survival curves and plot


library(survival)
fit <- survfit( Surv(time, status) ~ sex, data = lung )

library(survminer)
ggsurv <- ggsurvplot(fit, data = lung,
           risk.table = TRUE,
           cumevents = TRUE,
           palette = "jco",
           tables.height = 0.15, # specify tables height
           tables.y.text = FALSE, # hide tables y axis text 
           ggtheme = theme_gray(), # main theme
           tables.theme = theme_cleantable()
)
print(ggsurv)

rplot11

Get the data used to create survival curves


# Use this:
data.survplot <- ggsurv$data.survplot
# Or this:
data.survplot <- surv_summary(fit, data = lung)
head(data.survplot)
  time n.risk n.event n.censor      surv    std.err     upper     lower strata sex
1   11    138       3        0 0.9782609 0.01268978 1.0000000 0.9542301  sex=1   1
2   12    135       1        0 0.9710145 0.01470747 0.9994124 0.9434235  sex=1   1
3   13    134       2        0 0.9565217 0.01814885 0.9911586 0.9230952  sex=1   1
4   15    132       1        0 0.9492754 0.01967768 0.9866017 0.9133612  sex=1   1
5   26    131       1        0 0.9420290 0.02111708 0.9818365 0.9038355  sex=1   1
6   30    130       1        0 0.9347826 0.02248469 0.9768989 0.8944820  sex=1   1

Get the data used to create tables


risk table, cumulative number of event table, cumulative number of censoring table.

data.survtable <- ggsurv$data.survtable
data.survtable
   strata time n.risk pct.risk n.event cum.n.event n.censor cum.n.censor strata_size sex
1   sex=1    0    138      100       0           0        0            0         138   1
2   sex=1  250     62       45      65          65       11           11         138   1
3   sex=1  500     20       14      31          96       11           22         138   1
4   sex=1  750      7        5      13         109        0           22         138   1
5   sex=1 1000      2        1       3         112        2           24         138   1
6   sex=2    0     90      100       0           0        0            0          90   2
7   sex=2  250     53       59      24          24       13           13          90   2
8   sex=2  500     21       23      18          42       14           27          90   2
9   sex=2  750      3        3      10          52        8           35          90   2
10  sex=2 1000      0        0       1          53        2           37          90   2
> 
kassambara added a commit that referenced this issue Feb 28, 2017
kassambara added a commit that referenced this issue Feb 28, 2017
@kassambara kassambara closed this Feb 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.