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

A single legend for multiple ggsurvplots using arrange_ggsurvplot #421

Open
MaximePineaux opened this issue Aug 22, 2019 · 2 comments
Open

Comments

@MaximePineaux
Copy link

Expected behavior

First thank you for this great package. The same issue has been posted [#340] but without an answer yet. I would like to get four survival plots arranged in a grid with a common legend applied to the top (or bottom or right) of the grid.

Actual behavior

I can't figure out how to get a single legend for the four plot when using arrange_ggsurvplot. The only (bad) solution I find, is to remove the legend from all plots except the top right one but this makes it look smaller and uneven with the other plots. The least bad solution I find is to position the legend inside one of the plot but it depends on the data and it doesn't look nice.

Steps to reproduce the problem

require(survival)
require(survminer)

Bad solution 1

fit<- survfit(Surv(time, status) ~ sex, data = lung)
a=ggsurvplot(fit, data = lung, xlab="", title="A", legend="none", legend.title="Sex", legend.labs=c("Male","Female"))
b=ggsurvplot(fit, data = lung, title="B", legend="none", legend.title="Sex", legend.labs=c("Male","Female"))
c=ggsurvplot(fit, data = lung, xlab="",ylab="",title="C", legend="none", legend.title="Sex", legend.labs=c("Male","Female"))
d=ggsurvplot(fit, data = lung, title="D", ylab="",legend="none", legend.title="Sex", legend.labs=c("Male","Female"))

fits <- list(a = a, b = b,c = c, d = d)
arrange_ggsurvplots(fits, print = TRUE, ncol = 2, nrow = 2)

dummy1

And bad solution 2

c=ggsurvplot(fit, data = lung, title="C", legend=c(0.7,0.7), legend.title="Sex", legend.labs=c("Male","Female"))

fits <- list(a = a, b = b,c = c, d = d)
arrange_ggsurvplots(fits, print = TRUE, ncol = 2, nrow = 2)

dummy2

session_info()

- Session info -----------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.5.2 (2018-12-20)
 os       Windows >= 8 x64            
 system   x86_64, mingw32             
 ui       RStudio                     
 language (EN)                        
 collate  French_France.1252          
 ctype    French_France.1252          
 tz       Europe/Paris                
 date     2019-08-22                  

- Packages ---------------------------------------------------------------------------------------------
 package     * version date       lib source        
 assertthat    0.2.0   2017-04-11 [1] CRAN (R 3.5.1)
 backports     1.1.2   2017-12-13 [1] CRAN (R 3.5.0)
 bindr         0.1.1   2018-03-13 [1] CRAN (R 3.5.1)
 bindrcpp      0.2.2   2018-03-29 [1] CRAN (R 3.5.1)
 broom         0.5.0   2018-07-17 [1] CRAN (R 3.5.1)
 callr         3.3.1   2019-07-18 [1] CRAN (R 3.5.3)
 cli           1.1.0   2019-03-19 [1] CRAN (R 3.5.3)
 cmprsk        2.2-7   2014-06-17 [1] CRAN (R 3.5.1)
 colorspace    1.3-2   2016-12-14 [1] CRAN (R 3.5.1)
 crayon        1.3.4   2017-09-16 [1] CRAN (R 3.5.1)
 data.table    1.11.8  2018-09-30 [1] CRAN (R 3.5.1)
 desc          1.2.0   2018-05-01 [1] CRAN (R 3.5.3)
 devtools    * 2.1.0   2019-07-06 [1] CRAN (R 3.5.3)
 digest        0.6.17  2018-09-12 [1] CRAN (R 3.5.1)
 dplyr         0.7.6   2018-06-29 [1] CRAN (R 3.5.1)
 fs            1.3.1   2019-05-06 [1] CRAN (R 3.5.3)
 ggplot2     * 3.0.0   2018-07-03 [1] CRAN (R 3.5.1)
 ggpubr      * 0.1.8   2018-08-30 [1] CRAN (R 3.5.1)
 glue          1.3.0   2018-07-17 [1] CRAN (R 3.5.1)
 gridExtra     2.3     2017-09-09 [1] CRAN (R 3.5.1)
 gtable        0.2.0   2016-02-26 [1] CRAN (R 3.5.1)
 km.ci         0.5-2   2009-08-30 [1] CRAN (R 3.5.1)
 KMsurv        0.1-5   2012-12-03 [1] CRAN (R 3.5.0)
 knitr         1.20    2018-02-20 [1] CRAN (R 3.5.1)
 labeling      0.3     2014-08-23 [1] CRAN (R 3.5.0)
 lattice       0.20-38 2018-11-04 [2] CRAN (R 3.5.2)
 lazyeval      0.2.1   2017-10-29 [1] CRAN (R 3.5.1)
 magrittr    * 1.5     2014-11-22 [1] CRAN (R 3.5.1)
 Matrix        1.2-15  2018-11-01 [2] CRAN (R 3.5.2)
 memoise       1.1.0   2017-04-21 [1] CRAN (R 3.5.3)
 munsell       0.5.0   2018-06-12 [1] CRAN (R 3.5.1)
 nlme          3.1-137 2018-04-07 [2] CRAN (R 3.5.2)
 pillar        1.3.0   2018-07-14 [1] CRAN (R 3.5.1)
 pkgbuild      1.0.4   2019-08-05 [1] CRAN (R 3.5.3)
 pkgconfig     2.0.2   2018-08-16 [1] CRAN (R 3.5.1)
 pkgload       1.0.2   2018-10-29 [1] CRAN (R 3.5.3)
 plyr          1.8.4   2016-06-08 [1] CRAN (R 3.5.1)
 prettyunits   1.0.2   2015-07-13 [1] CRAN (R 3.5.3)
 processx      3.4.1   2019-07-18 [1] CRAN (R 3.5.3)
 ps            1.3.0   2018-12-21 [1] CRAN (R 3.5.3)
 purrr         0.2.5   2018-05-29 [1] CRAN (R 3.5.1)
 R6            2.2.2   2017-06-17 [1] CRAN (R 3.5.1)
 Rcpp          0.12.19 2018-10-01 [1] CRAN (R 3.5.1)
 remotes       2.1.0   2019-06-24 [1] CRAN (R 3.5.3)
 rlang         0.4.0   2019-06-25 [1] CRAN (R 3.5.3)
 rprojroot     1.3-2   2018-01-03 [1] CRAN (R 3.5.3)
 rstudioapi    0.10    2019-03-19 [1] CRAN (R 3.5.3)
 scales        1.0.0   2018-08-09 [1] CRAN (R 3.5.1)
 sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 3.5.3)
 survival    * 2.43-3  2018-11-26 [2] CRAN (R 3.5.2)
 survminer   * 0.4.3   2018-08-04 [1] CRAN (R 3.5.1)
 survMisc      0.5.5   2018-07-05 [1] CRAN (R 3.5.1)
 testthat      2.2.1   2019-07-25 [1] CRAN (R 3.5.3)
 tibble        1.4.2   2018-01-22 [1] CRAN (R 3.5.1)
 tidyr         0.8.1   2018-05-18 [1] CRAN (R 3.5.1)
 tidyselect    0.2.4   2018-02-26 [1] CRAN (R 3.5.1)
 usethis     * 1.5.1   2019-07-04 [1] CRAN (R 3.5.3)
 withr         2.1.2   2018-03-15 [1] CRAN (R 3.5.1)
 xtable        1.8-3   2018-08-29 [1] CRAN (R 3.5.1)
 zoo           1.8-4   2018-09-19 [1] CRAN (R 3.5.1)
@cwilso6
Copy link

cwilso6 commented Mar 23, 2021

Any updates?

@philiph99
Copy link

This might work (adapted from a Stack Overflow post):
library(patchwork)
(a$plot + b$plot + c$plot + d$plot) / guide_area() +
plot_layout( guides = 'collect')

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

3 participants