Skip to content

Continuous fill does not obey fine-grained show.legend settings #3461

Closed
@mjskay

Description

@mjskay

If you use fine-grained settings with show.legend, the legend for a continuous fill variable is not drawn. E.g. this:

library(ggplot2)
library(dplyr)

tribble(
  ~x1, ~y1, ~x2, ~y2, ~id, ~id_int,
    1,   1,   2,   2, "A",       1,
    2,   2,   3,   3, "B",       2
) %>%
  ggplot(aes(xmin = x1, ymin = y1, xmax = x2, ymax = y2, fill = id_int)) +
  # could also do show.legend = c(size = FALSE) here and get the same
  # result; in both cases I would have expected the fill legend to show up
  geom_rect(show.legend = c(size = FALSE, fill = TRUE))

Created on 2019-07-28 by the reprex package (v0.3.0)

I chose size here as an unrelated variable, it could be anything. This problem also happens on geoms other than rect, so I don't think it's a problem there.

I would expect output that looks like this:

image

FWIW, with a discrete variable for the fill, it does work as expected:

library(ggplot2)
library(dplyr)

tribble(
  ~x1, ~y1, ~x2, ~y2, ~id, ~id_int,
    1,   1,   2,   2, "A",       1,
    2,   2,   3,   3, "B",       2
) %>%
  ggplot(aes(xmin = x1, ymin = y1, xmax = x2, ymax = y2, fill = id)) +
  # could also do show.legend = c(size = FALSE) here and get the correct result
  geom_rect(show.legend = c(size = FALSE, fill = TRUE))

Created on 2019-07-28 by the reprex package (v0.3.0)

I tested on the github version of ggplot2 as of today (2019-07-28). Session info:

R version 3.6.0 (2019-04-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

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

other attached packages:
 [1] reprex_0.3.0          gganimate_1.0.3       RColorBrewer_1.1-2    cowplot_1.0.0         forcats_0.4.0        
 [6] modelr_0.1.4          brms_2.9.0            Rcpp_1.0.2            broom_0.5.2           emmeans_1.3.5.1      
[11] tidybayes_1.1.0       rstan_2.19.2          StanHeaders_2.18.1-10 ggstance_0.3.2        ggplot2_3.2.0.9000   
[16] dplyr_0.8.3           magrittr_1.5         

loaded via a namespace (and not attached):
  [1] ggnewscale_0.3.0          TH.data_1.0-10            colorspace_1.4-1          ggridges_0.5.1           
  [5] rsconnect_0.8.15          rprojroot_1.3-2           estimability_1.3          markdown_1.0             
  [9] base64enc_0.1-3           fs_1.3.1                  rstudioapi_0.10           farver_1.1.0             
 [13] remotes_2.1.0             svUnit_0.7-12             DT_0.7                    mvtnorm_1.0-11           
 [17] dotwhisker_0.5.0          bridgesampling_0.7-2      codetools_0.2-16          splines_3.6.0            
 [21] knitr_1.23                shinythemes_1.1.2         zeallot_0.1.0             pkgload_1.0.2            
 [25] bayesplot_1.7.0           packrat_0.5.0             shiny_1.3.2               clipr_0.7.0              
 [29] compiler_3.6.0            backports_1.1.4           assertthat_0.2.1          Matrix_1.2-17            
 [33] lazyeval_0.2.2            cli_1.1.0                 tweenr_1.0.1              later_0.8.0              
 [37] htmltools_0.3.6           prettyunits_1.0.2         tools_3.6.0               igraph_1.2.4.1           
 [41] coda_0.19-3               gtable_0.3.0              glue_1.3.1                reshape2_1.4.3           
 [45] vctrs_0.2.0               nlme_3.1-140              crosstalk_1.0.0           xfun_0.8                 
 [49] stringr_1.4.0             ps_1.3.0                  mime_0.7                  miniUI_0.1.1.1           
 [53] gtools_3.8.1              devtools_2.1.0            MASS_7.3-51.4             zoo_1.8-6                
 [57] scales_1.0.0              colourpicker_1.0          hms_0.5.0                 promises_1.0.1           
 [61] Brobdingnag_1.2-6         parallel_3.6.0            sandwich_2.5-1            inline_0.3.15            
 [65] shinystan_2.5.0           curl_4.0                  memoise_1.1.0             gridExtra_2.3            
 [69] loo_2.1.0                 stringi_1.4.3             dygraphs_1.1.1.6          desc_1.2.0               
 [73] pkgbuild_1.0.3            rlang_0.4.0               pkgconfig_2.0.2           matrixStats_0.54.0       
 [77] evaluate_0.14             lattice_0.20-38           purrr_0.3.2               labeling_0.3             
 [81] rstantools_1.5.1          htmlwidgets_1.3           processx_3.4.1            tidyselect_0.2.5         
 [85] plyr_1.8.4                R6_2.4.0                  generics_0.0.2            multcomp_1.4-10          
 [89] whisker_0.3-2             pillar_1.4.2              withr_2.1.2               xts_0.11-2               
 [93] survival_2.44-1.1         abind_1.4-5               tibble_2.1.3              crayon_1.3.4             
 [97] arrayhelpers_1.0-20160527 rmarkdown_1.14            progress_1.2.2            usethis_1.5.1            
[101] grid_3.6.0                callr_3.3.1               threejs_0.3.1             digest_0.6.20            
[105] xtable_1.8-4              tidyr_0.8.3               httpuv_1.5.1              stats4_3.6.0             
[109] munsell_0.5.0             sessioninfo_1.1.1         shinyjs_1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions