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

When data is in tibble format, ggsurvplot_facet will give an error with pval = TRUE #670

Open
kaipingyang opened this issue Dec 4, 2024 · 0 comments

Comments

@kaipingyang
Copy link

kaipingyang commented Dec 4, 2024

When data is in tibble format, ggsurvplot_facet's pval = TRUE gives the following error:
Error in xtfrm.data.frame(x) : cannot xtfrm data frames

Here's a reproducible example

library(survival)
library(survminer)
library(tidyverse)
test <- colon %>% as_tibble()
fit <- survfit( Surv(time, status) ~ sex, data = test)
ggsurvplot_facet(fit, test, facet.by = "rx",
                 palette = "jco", pval = TRUE)

#> Error in xtfrm.data.frame(x) : cannot xtfrm data frames

I looked at the source code and found that ggsurvplot_facet calls surv_group_by when pval = TRUE, and in surv_group_by there is the following:

  if(length(grouping.vars) == 1){
    grp.levels <- .levels(data[, grouping.vars])
    current.order <- grouped.d[, grouping.vars] %>% as.data.frame() %>% .[,1]
    grouped.d <- grouped.d[match(grp.levels, current.order), , drop = FALSE]
  }

Notice that data[, grouping.vars] gives inconsistent results for dataframe and tibble:
The former is a vector;
The latter is tibble.
Here's a reproducible example:

> colon[,"rx"] %>% class()
[1] "factor"
> test[,"rx"] %>% class()
[1] "tbl_df"     "tbl"        "data.frame"

survminer:::.levels() can only operate on vectors, which is the cause of the error.

survminer:::.levels(test[,"rx"])
#> Error in xtfrm.data.frame(x) : cannot xtfrm data frames

So I suggest changing grp.levels <- .levels(data[, grouping.vars]) to grp.levels <- .levels(data[[grouping.vars]]) when length(grouping.vars) == 1 in surv_group_by .

session_info()

> sessionInfo()
R version 4.4.1 (2024-06-14 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 22621)

Matrix products: default


locale:
[1] LC_COLLATE=Chinese (Simplified)_China.utf8  LC_CTYPE=Chinese (Simplified)_China.utf8   
[3] LC_MONETARY=Chinese (Simplified)_China.utf8 LC_NUMERIC=C                               
[5] LC_TIME=Chinese (Simplified)_China.utf8    

time zone: Asia/Shanghai
tzcode source: internal

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] lubridate_1.9.3    forcats_1.0.0      stringr_1.5.1      dplyr_1.1.4        purrr_1.0.2       
 [6] readr_2.1.5        tidyr_1.3.1        tibble_3.2.1       tidyverse_2.0.0    survival_3.6-4    
[11] survminer_0.5.0    ggpubr_0.6.0       ggplot2_3.5.1      gtable_0.3.5       gridExtra_2.3     
[16] forestploter_1.1.3

loaded via a namespace (and not attached):
 [1] xfun_0.45          bslib_0.7.0        teal.widgets_0.4.2 rstatix_0.7.2      lattice_0.22-6    
 [6] tzdb_0.4.0         vctrs_0.6.5        tools_4.4.1        generics_0.1.3     fansi_1.0.6       
[11] pkgconfig_2.0.3    Matrix_1.7-0       data.table_1.15.4  lifecycle_1.0.4    compiler_4.4.1    
[16] farver_2.1.2       munsell_0.5.1      ggsci_3.2.0        carData_3.0-5      httpuv_1.6.15     
[21] shinyWidgets_0.8.6 htmltools_0.5.8.1  sass_0.4.9         later_1.3.2        pillar_1.9.0      
[26] car_3.1-2          jquerylib_0.1.4    cachem_1.1.0       abind_1.4-5        mime_0.12         
[31] km.ci_0.5-6        tidyselect_1.2.1   digest_0.6.36      stringi_1.8.4      labeling_0.4.3    
[36] splines_4.4.1      fastmap_1.2.0      colorspace_2.1-0   cli_3.6.3          magrittr_2.0.3    
[41] cards_0.3.0        utf8_1.2.4         broom_1.0.6        withr_3.0.0        scales_1.3.0      
[46] promises_1.3.0     backports_1.5.0    timechange_0.3.0   ggsignif_0.6.4     hms_1.1.3         
[51] zoo_1.8-12         memoise_2.0.1      shiny_1.9.1        knitr_1.47         KMsurv_0.1-5      
[56] survMisc_0.5.6     rlang_1.1.4        Rcpp_1.0.12        xtable_1.8-4       glue_1.8.0        
[61] rstudioapi_0.16.0  jsonlite_1.8.8     R6_2.5.1 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant