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

ggsurvplot:: Placing risk table inside survival curves #69

Closed
kassambara opened this issue Oct 21, 2016 · 11 comments
Closed

ggsurvplot:: Placing risk table inside survival curves #69

kassambara opened this issue Oct 21, 2016 · 11 comments

Comments

@kassambara
Copy link
Owner

How to place risk table inside survival curves?

@MarcinKosinski
Copy link
Contributor

Maybe stacked barplot on X axis with additional legend on a right (2 legends :P)?

@kassambara
Copy link
Owner Author

Good idea!!

@Sterniii3
Copy link

How can I place the risk table inside the plot of the survival curves? Is it possible? If so, I can't find how to do it.
Thanks!

@kassambara
Copy link
Owner Author

This is not yet possible. We'll work on it:-)!

@kassambara
Copy link
Owner Author

A first attempt of placing risk.table inside survival curves:

library(survival)
library(survminer)

fit<- survfit(Surv(time, status) ~ sex, data = lung)
p<-ggsurvplot(fit, data = lung, risk.table = TRUE, risk.table.col = "strata")

# Create a transparent theme object
transparent_theme <- theme(
  axis.title.x = element_blank(),
  axis.title.y = element_blank(),
  axis.text.x = element_blank(),
  axis.text.y = element_blank(),
  axis.ticks = element_blank(),
  panel.grid = element_blank(),
  axis.line = element_blank(),
  panel.background = element_rect(fill = "transparent",colour = NA),
  plot.background = element_rect(fill = "transparent",colour = NA),
  plot.margin=unit(c(0,0,0,0),"mm"),)

p1 <- p$plot
p2 <- p$table + transparent_theme

# Create the external graphical elements
# called a "grop" in Grid terminology
p2_grob = ggplotGrob(p2)
p1 + annotation_custom(grob = p2_grob, xmin = -50, 
                       ymin = 0, ymax = 0.25)

rplot

I'm not sure that this is good idea...

@MarcinKosinski
Copy link
Contributor

It's always just an option : )
Maybe some users will find it useful (for plots with estimates above 50%).

I would go even further and try placing the legend inside the plot oO

@kassambara
Copy link
Owner Author

I think that for legend, the job is done using the argument `legend'

ggsurvplot(fit, data = lung, risk.table = TRUE, risk.table.col = "strata", 
          legend = c(0.2, 0.2))

@kassambara
Copy link
Owner Author

kassambara commented Mar 1, 2017

Optimal code to be added

# Fit survival curves
library(survival)
fit <- survfit( Surv(time, status) ~ rx + adhere, data = colon )

library(survminer)
p <- ggsurvplot(fit, data = lung, risk.table = TRUE, risk.table.col = "strata")

# Create a transparent theme object
transparent_theme <- theme(
  title = element_blank(),
  axis.title.x = element_blank(),
  axis.title.y = element_blank(),
  axis.text.x = element_blank(),
  axis.text.y = element_blank(),
  axis.ticks = element_blank(),
  panel.grid = element_blank(),
  axis.line = element_blank(),
  panel.background = element_rect(fill = "transparent",colour = NA),
  plot.background = element_rect(fill = "transparent",colour = NA),
  plot.margin=unit(c(0,0,0,0),"mm"),
  panel.border = element_blank(),
  legend.position = "none")

p1 <- p$plot
p2 <- p$table + transparent_theme

# Create the external graphical elements
# called a "grop" in Grid terminology
nstrata = 6
ymax <- nstrata*0.05
p2_grob = ggplotGrob(p2)
p1 + annotation_custom(grob = p2_grob, xmin = -max(fit$time)/20, 
                       ymin = -0.05, ymax = ymax)

rplot14

@kassambara
Copy link
Owner Author

option to add table tick labels on the right side

kassambara added a commit that referenced this issue Mar 1, 2017
kassambara added a commit that referenced this issue Mar 1, 2017
@kassambara
Copy link
Owner Author

New argument risk.table.pos in ggsurvplot() for placing risk table inside survival curves. Allowed options are one of c("out", "in") indicating 'outside' or 'inside' the main plot, respectively. Default value is "out".

library(survival)
fit <- survfit( Surv(time, status) ~ rx + adhere, data = colon )

library(survminer)
ggsurvplot(fit, data = colon, 
           legend = "right",
           pval = TRUE, pval.coord = c(500, 0.4),
           risk.table = TRUE, risk.table.y.text = FALSE,
           risk.table.pos = "in")

rplot15

@awcm0n
Copy link

awcm0n commented Nov 8, 2019

I like the option to put the risk table inside, but how can I get the table title back?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants