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

Lost ability to assign same color for same group accross facets #185

Closed
Eliford opened this issue Apr 5, 2017 · 4 comments
Closed

Lost ability to assign same color for same group accross facets #185

Eliford opened this issue Apr 5, 2017 · 4 comments

Comments

@Eliford
Copy link

Eliford commented Apr 5, 2017

Expected behavior

In the previous version of survminer, the code below would give a plot with only 7 colors each for a molecular group. I liked it because the a molecular group had a same color across facets. It was then easy to compare survival curves across facets.

library(tidyverse)
library(survminer)
library(survival)
myeloma<-myeloma
myeloma<-myeloma%>%mutate(type=sample(c(1,2), size=nrow(myeloma), replace=TRUE))

modelfit<-survfit(Surv(time, event)~molecular_group+type, data=myeloma)

modelfitplot<-ggsurvplot(modelfit, data = myeloma, 
                         legend.title = "Type", 
                         legend.labs = c("Cyclin D-1","Cyclin D-2","Hyperdiploid","Low bone disease","MAF","MMSET","Proliferation",
                                         "Cyclin D-1","Cyclin D-2","Hyperdiploid","Low bone disease","MAF","MMSET","Proliferation"),
                         palette = c("#F8766D","#E38900","#C49A00","#99A800","#53B400","#00BC56","#00C094",
                                     "#F8766D","#E38900","#C49A00","#99A800","#53B400","#00BC56","#00C094",))

modelfitplot$plot+facet_wrap(~type)

Actual behavior

With the current version the above code gives the error below

> modelfitplot<-ggsurvplot(modelfit, data = myeloma, 
+                          legend.title = "Type", 
+                          legend.labs = c("Cyclin D-1","Cyclin D-2","Hyperdiploid","Low bone disease","MAF","MMSET","Proliferation",
+                                          "Cyclin D-1","Cyclin D-2","Hyperdiploid","Low bone disease","MAF","MMSET","Proliferation"),
+                          palette = c("#F8766D","#E38900","#C49A00","#99A800","#53B400","#00BC56","#00C094",
+                                      "#F8766D","#E38900","#C49A00","#99A800","#53B400","#00BC56","#00C094"))
Error in names(scurve_cols) <- legend.labs : 
  'names' attribute [14] must be the same length as the vector [7]
In addition: Warning messages:
1: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else paste0(labels,  :
  duplicated levels in factors are deprecated
2: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else paste0(labels,  :
  duplicated levels in factors are deprecated
3: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else paste0(labels,  :
  duplicated levels in factors are deprecated
4: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else paste0(labels,  :
  duplicated levels in factors are deprecated
5: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else paste0(labels,  :
  duplicated levels in factors are deprecated

Steps to reproduce the problem

The dataset above comes with the survminer package. The code above shows how I was able to reproduce the problem that I observed with my data.

session_info()

# please paste here the result of
> devtools::session_info()
Session info ----------------------------------------------------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.3.3 (2017-03-06)
 system   x86_64, linux-gnu           
 ui       RStudio (1.1.122)           
 language (EN)                        
 collate  en_US.UTF-8                 
 tz       posixrules                  
 date     2017-04-05                  

Packages --------------------------------------------------------------------------------------------------------------------------------------------
 package    * version    date       source                             
 assertthat   0.1        2013-12-06 CRAN (R 3.3.1)                     
 bindr        0.1        2016-11-13 cran (@0.1)                        
 bindrcpp   * 0.1        2016-12-11 cran (@0.1)                        
 broom        0.4.2      2017-02-13 CRAN (R 3.3.1)                     
 cmprsk       2.2-7      2014-06-17 CRAN (R 3.3.1)                     
 colorspace   1.3-2      2016-12-14 CRAN (R 3.3.1)                     
 data.table   1.10.4     2017-02-01 CRAN (R 3.3.1)                     
 devtools     1.12.0     2016-06-24 CRAN (R 3.3.1)                     
 digest       0.6.12     2017-01-27 CRAN (R 3.3.1)                     
 dplyr      * 0.5.0.9001 2017-04-04 Github (hadley/dplyr@2bdb19d)      
 forcats      0.2.0      2017-01-23 cran (@0.2.0)                      
 foreign      0.8-67     2016-09-13 CRAN (R 3.3.1)                     
 ggplot2    * 2.2.1      2016-12-30 CRAN (R 3.3.1)                     
 ggpubr     * 0.1.2      2017-03-14 CRAN (R 3.3.3)                     
 glue         0.0.0.9000 2017-04-04 Github (tidyverse/glue@6e77c3b)    
 gridExtra    2.2.1      2016-02-29 CRAN (R 3.3.1)                     
 gtable       0.2.0      2016-02-26 CRAN (R 3.3.1)                     
 haven        1.0.0.9000 2017-02-17 Github (hadley/haven@4cb0d51)      
 hms          0.3        2016-11-22 CRAN (R 3.3.1)                     
 httr         1.2.1      2016-07-03 CRAN (R 3.3.1)                     
 jsonlite     1.3        2017-02-28 CRAN (R 3.3.3)                     
 km.ci        0.5-2      2009-08-30 CRAN (R 3.3.1)                     
 KMsurv       0.1-5      2012-12-03 CRAN (R 3.3.1)                     
 knitr        1.15.1     2016-11-22 CRAN (R 3.3.1)                     
 labeling     0.3        2014-08-23 CRAN (R 3.3.1)                     
 lattice      0.20-34    2016-09-06 CRAN (R 3.3.1)                     
 lazyeval     0.2.0.9000 2017-03-24 Github (hadley/lazyeval@c155c3d)   
 lubridate    1.6.0      2016-09-13 CRAN (R 3.3.1)                     
 magrittr     1.5        2017-03-27 Github (tidyverse/magrittr@cf2e33f)
 Matrix       1.2-8      2017-01-20 CRAN (R 3.3.2)                     
 memoise      1.0.0      2016-01-29 CRAN (R 3.3.1)                     
 mnormt       1.5-5      2016-10-15 CRAN (R 3.3.1)                     
 modelr       0.1.0      2016-08-31 CRAN (R 3.3.3)                     
 munsell      0.4.3      2016-02-13 CRAN (R 3.3.1)                     
 nlme         3.1-131    2017-02-06 CRAN (R 3.3.2)                     
 plyr         1.8.4      2016-06-08 CRAN (R 3.3.1)                     
 psych        1.6.12     2017-01-08 CRAN (R 3.3.1)                     
 purrr      * 0.2.2      2016-06-18 CRAN (R 3.3.1)                     
 R6           2.2.0      2016-10-05 CRAN (R 3.3.1)                     
 Rcpp         0.12.10    2017-04-04 Github (RcppCore/Rcpp@886f5df)     
 readr      * 1.1.0      2017-03-22 cran (@1.1.0)                      
 readxl       0.1.1      2016-03-28 CRAN (R 3.3.3)                     
 reshape2     1.4.2      2016-10-22 CRAN (R 3.3.1)                     
 rlang        0.0.0.9006 2017-04-04 Github (hadley/rlang@0756eaf)      
 rvest        0.3.2      2016-06-17 CRAN (R 3.3.3)                     
 scales       0.4.1      2016-11-09 CRAN (R 3.3.1)                     
 stringi      1.1.3      2017-03-21 cran (@1.1.3)                      
 stringr      1.2.0      2017-02-18 CRAN (R 3.3.3)                     
 survival   * 2.41-2     2017-03-16 CRAN (R 3.3.3)                     
 survminer  * 0.3.0      2017-03-16 CRAN (R 3.3.3)                     
 survMisc     0.5.4      2016-11-23 CRAN (R 3.3.3)                     
 tibble     * 1.3.0      2017-04-01 cran (@1.3.0)                      
 tidyr      * 0.6.1.9000 2017-03-24 Github (tidyverse/tidyr@0f9a5da)   
 tidyverse  * 1.1.1.9000 2017-03-27 Github (hadley/tidyverse@f27f74c)  
 withr        1.0.2      2016-06-20 CRAN (R 3.3.1)                     
 xml2         1.1.1      2017-01-24 CRAN (R 3.3.3)                     
 xtable       1.8-2      2016-02-05 CRAN (R 3.3.1)                     
 yaml         2.1.14     2016-11-12 CRAN (R 3.3.1)                     
 zoo          1.7-14     2016-12-19 CRAN (R 3.3.1) 
@kassambara
Copy link
Owner

Thank you for pointing this out. Fixed now.

Observation: you're using a hack to color curves by molecular_group accross facets. In order order to have the same color in all facets, you duplicated the levels of legend labels and color palettes. This is not recommended, because doing so, you'll always have the following warning:

Warning message:
In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else paste0(labels,  :
  duplicated levels in factors are deprecated

suggestion: By default, survival curves are colored by strata using the argument color = "strata", but you can also color the curves by any other grouping variables used to fit the survival curves. In your example, you can color the survival curves by molecular_group using color = "molecular_group".

This has been now clarified in the documentation of ggsurvplot()as follow:

  • argument color: color to be used for the survival curves.
    • If the number of strata/group (n.strata) = 1, the expected value is the color name. For example color = "blue".
    • If n.strata > 1, the expected value is the grouping variable name. By default, survival curves are colored by strata using the argument color = "strata", but you can also color survival curves by any other grouping variables used to fit the survival curves.

Install the latest version as follow:

if(!require(devtools)) install.packages("devtools")
devtools::install_github("kassambara/survminer", build_vignettes = FALSE)

Then, try this:

library(tidyverse)
library(survminer)
library(survival)
myeloma<-myeloma
myeloma<-myeloma%>%mutate(type=sample(c(1,2), size=nrow(myeloma), replace=TRUE))

modelfit<-survfit(Surv(time, event)~molecular_group+type, data=myeloma)

# To visualize, use this
ggsurvplot(modelfit, data = myeloma, 
           legend.title = "Type", 
           color = "molecular_group",
           palette = c("#F8766D","#E38900","#C49A00","#99A800","#53B400","#00BC56","#00C094")
           ) %>%
  .$plot+facet_wrap(~type)

# Or this
ggsurvplot(modelfit, data = myeloma, 
           legend.title = "Type", 
           color = "molecular_group",
           palette = "jco") %>%
  .$plot+facet_wrap(~type)

rplot

Please, let us know if it works for you so that we can close the issue

kassambara added a commit that referenced this issue Apr 6, 2017
kassambara added a commit that referenced this issue Apr 6, 2017
@kassambara
Copy link
Owner

@Eliford did this work?

@Eliford
Copy link
Author

Eliford commented Apr 10, 2017

@kassambara .
Yes, it worked after I updated the package.
Thank you very much.

@kassambara
Copy link
Owner

Oh, great! So we can close this issue.

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

2 participants