kassambara / survminer

Survival Analysis and Visualization
https://rpkgs.datanovia.com/survminer/
509 stars 164 forks source link

legend.labs and risk table for multiple groups for cox regression #541

Open SunsetBlvd opened 3 years ago

SunsetBlvd commented 3 years ago

Expected behavior

I fitted Cox regression and would like to plot a graph with 4 age groups and risk table for them. I expect to see in a risk table 4 SEPARATE counts for each age group (number at risk). So, the question is how to get risk table with 4 SEPARATE rows (with numbers for each of the 4 age groups).

Actual behavior

But I get only JOINT counts for all groups plus ggsurvplot function prints a warning: "The length of legend.labs should be 1".

If I reduce legend.labs down to 1 value the warning changes to: "The length of legend.labs should be 4".

Steps to reproduce the problem

library(survival)
library(survminer)

data = pbc
data$status[data$status == 1] = 0
data$status[data$status == 2] = 1

data$trt[data$trt == 2] = 0
data = data[!is.na(data$trt),]
data$trt = factor(data$trt)

data$age.cat = " 1. <42"
data$age.cat[data$age >= 42 & data$age < 50] = " 2. [42, 50)"
data$age.cat[data$age >= 50 & data$age < 57] = " 3. [50, 57)"
data$age.cat[data$age >= 57] = " 4. >= 57"
data$age.cat = factor(data$age.cat)

cox.reg = coxph(Surv(time, status) ~ trt + age.cat + sex, data = data)

data_test2 = data.frame( age.cat = factor(c(" 1. <42", " 2. [42, 50)", " 3. [50, 57)", " 4. >= 57")),
                         sex = factor(c("f", "f", "f", "f"), levels = c("f", "m")),
                         trt = factor(c(1, 1, 1, 1), levels = c(0, 1)) )

sf_test2 = survfit(cox.reg, newdata = data_test2)

ggsurvplot(sf_test2, title = "Females, DPCA, Low Bilirubin, 4 age groups",
legend.title = "Age groups",
risk.table = TRUE, 
legend.labs = c("1. <42", "2. [42, 50)", "[50, 57)", "4. >= 57"),
conf.int = F, censor = F, data = data)

session_info()

- Session info ---------------------------------------------------------------
 setting  value                       
 version  R version 4.0.5 (2021-03-31)
 os       Windows 10 x64              
 system   x86_64, mingw32             
 ui       Rgui                        
 language (EN)                        
 collate  English_United States.1252  
 ctype    English_United States.1252  
 tz       Asia            
 date     2021-06-26                  

- Packages -------------------------------------------------------------------
 package     * version date       lib source        
 abind         1.4-5   2016-07-21 [1] CRAN (R 4.0.3)
 assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.0.5)
 backports     1.2.1   2020-12-09 [1] CRAN (R 4.0.3)
 broom         0.7.6   2021-04-05 [1] CRAN (R 4.0.5)
 cachem        1.0.4   2021-02-13 [1] CRAN (R 4.0.5)
 callr         3.7.0   2021-04-20 [1] CRAN (R 4.0.5)
 car           3.0-10  2020-09-29 [1] CRAN (R 4.0.5)
 carData       3.0-4   2020-05-22 [1] CRAN (R 4.0.3)
 cellranger    1.1.0   2016-07-27 [1] CRAN (R 4.0.5)
 cli           2.4.0   2021-04-05 [1] CRAN (R 4.0.5)
 colorspace    2.0-0   2020-11-11 [1] CRAN (R 4.0.5)
 crayon        1.4.1   2021-02-08 [1] CRAN (R 4.0.5)
 curl          4.3     2019-12-02 [1] CRAN (R 4.0.5)
 data.table    1.14.0  2021-02-21 [1] CRAN (R 4.0.5)
 DBI           1.1.1   2021-01-15 [1] CRAN (R 4.0.5)
 desc          1.3.0   2021-03-05 [1] CRAN (R 4.0.5)
 devtools      2.4.1   2021-05-05 [1] CRAN (R 4.0.5)
 digest        0.6.27  2020-10-24 [1] CRAN (R 4.0.5)
 dplyr         1.0.5   2021-03-05 [1] CRAN (R 4.0.5)
 ellipsis      0.3.1   2020-05-15 [1] CRAN (R 4.0.5)
 fansi         0.4.2   2021-01-15 [1] CRAN (R 4.0.5)
 farver        2.1.0   2021-02-28 [1] CRAN (R 4.0.5)
 fastmap       1.1.0   2021-01-25 [1] CRAN (R 4.0.5)
 forcats       0.5.1   2021-01-27 [1] CRAN (R 4.0.5)
 foreign       0.8-81  2020-12-22 [2] CRAN (R 4.0.5)
 fs            1.5.0   2020-07-31 [1] CRAN (R 4.0.5)
 generics      0.1.0   2020-10-31 [1] CRAN (R 4.0.5)
 ggplot2     * 3.3.3   2020-12-30 [1] CRAN (R 4.0.5)
 ggpubr      * 0.4.0   2020-06-27 [1] CRAN (R 4.0.5)
 ggsignif      0.6.1   2021-02-23 [1] CRAN (R 4.0.5)
 ggtext        0.1.1   2020-12-17 [1] CRAN (R 4.0.5)
 glue          1.4.2   2020-08-27 [1] CRAN (R 4.0.5)
 gridExtra     2.3     2017-09-09 [1] CRAN (R 4.0.5)
 gridtext      0.1.4   2020-12-10 [1] CRAN (R 4.0.5)
 gtable        0.3.0   2019-03-25 [1] CRAN (R 4.0.5)
 haven         2.4.1   2021-04-23 [1] CRAN (R 4.0.5)
 hms           1.0.0   2021-01-13 [1] CRAN (R 4.0.5)
 km.ci         0.5-2   2009-08-30 [1] CRAN (R 4.0.5)
 KMsurv        0.1-5   2012-12-03 [1] CRAN (R 4.0.3)
 knitr         1.32    2021-04-14 [1] CRAN (R 4.0.5)
 labeling      0.4.2   2020-10-20 [1] CRAN (R 4.0.3)
 lattice       0.20-41 2020-04-02 [2] CRAN (R 4.0.5)
 lifecycle     1.0.0   2021-02-15 [1] CRAN (R 4.0.5)
 magrittr      2.0.1   2020-11-17 [1] CRAN (R 4.0.5)
 markdown      1.1     2019-08-07 [1] CRAN (R 4.0.5)
 Matrix        1.3-2   2021-01-06 [2] CRAN (R 4.0.5)
 memoise       2.0.0   2021-01-26 [1] CRAN (R 4.0.5)
 munsell       0.5.0   2018-06-12 [1] CRAN (R 4.0.5)
 openxlsx      4.2.3   2020-10-27 [1] CRAN (R 4.0.5)
 pillar        1.6.0   2021-04-13 [1] CRAN (R 4.0.5)
 pkgbuild      1.2.0   2020-12-15 [1] CRAN (R 4.0.5)
 pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.0.5)
 pkgload       1.2.1   2021-04-06 [1] CRAN (R 4.0.5)
 prettyunits   1.1.1   2020-01-24 [1] CRAN (R 4.0.5)
 processx      3.5.1   2021-04-04 [1] CRAN (R 4.0.5)
 ps            1.6.0   2021-02-28 [1] CRAN (R 4.0.5)
 purrr         0.3.4   2020-04-17 [1] CRAN (R 4.0.5)
 R6            2.5.0   2020-10-28 [1] CRAN (R 4.0.5)
 Rcpp          1.0.6   2021-01-15 [1] CRAN (R 4.0.4)
 readxl        1.3.1   2019-03-13 [1] CRAN (R 4.0.5)
 remotes       2.3.0   2021-04-01 [1] CRAN (R 4.0.5)
 rio           0.5.26  2021-03-01 [1] CRAN (R 4.0.5)
 rlang         0.4.10  2020-12-30 [1] CRAN (R 4.0.5)
 rprojroot     2.0.2   2020-11-15 [1] CRAN (R 4.0.5)
 rstatix       0.7.0   2021-02-13 [1] CRAN (R 4.0.5)
 scales        1.1.1   2020-05-11 [1] CRAN (R 4.0.5)
 sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 4.0.5)
 stringi       1.5.3   2020-09-09 [1] CRAN (R 4.0.3)
 stringr       1.4.0   2019-02-10 [1] CRAN (R 4.0.5)
 survival    * 3.2-10  2021-03-16 [2] CRAN (R 4.0.5)
 survminer   * 0.4.9   2021-03-09 [1] CRAN (R 4.0.5)
 survMisc      0.5.5   2018-07-05 [1] CRAN (R 4.0.5)
 testthat      3.0.2   2021-02-14 [1] CRAN (R 4.0.5)
 tibble        3.1.0   2021-02-25 [1] CRAN (R 4.0.5)
 tidyr         1.1.3   2021-03-03 [1] CRAN (R 4.0.5)
 tidyselect    1.1.0   2020-05-11 [1] CRAN (R 4.0.5)
 usethis       2.0.1   2021-02-10 [1] CRAN (R 4.0.5)
 utf8          1.2.1   2021-03-12 [1] CRAN (R 4.0.5)
 vctrs         0.3.7   2021-03-29 [1] CRAN (R 4.0.5)
 withr         2.4.1   2021-01-26 [1] CRAN (R 4.0.5)
 xfun          0.22    2021-03-11 [1] CRAN (R 4.0.5)
 xml2          1.3.2   2020-04-23 [1] CRAN (R 4.0.5)
 xtable        1.8-4   2019-04-21 [1] CRAN (R 4.0.5)
 zip           2.1.1   2020-08-27 [1] CRAN (R 4.0.5)
 zoo           1.8-9   2021-03-09 [1] CRAN (R 4.0.5)
caroline-margiotta commented 2 months ago

@SunsetBlvd @kassambara just want to say I am noticing this issue as well. Is there a workaround that folks have discovered?