Skip to content

Default aesthetics for stats can create unexpected legend titles #2406

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

Closed
datalorax opened this issue Jan 17, 2018 · 7 comments · Fixed by #4414
Closed

Default aesthetics for stats can create unexpected legend titles #2406

datalorax opened this issue Jan 17, 2018 · 7 comments · Fixed by #4414
Labels
bug an unexpected problem or unintended behavior scales 🐍
Milestone

Comments

@datalorax
Copy link

I've run into some unexpected behavior when using geom_density_2d.

If I run

library(ggplot2)
ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  geom_smooth(aes(color = drv)) 

The legend title corresponds to the variable name (drv).
screen shot 2018-01-17 at 11 55 02 am

But if I add a geom_density_2d layer that I want plotted under the smooths, the legend title changes to "colour".

ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  geom_density2d() +
  geom_smooth(aes(color = drv)) 

screen shot 2018-01-17 at 11 57 24 am

I'm aware that I could change this manually with guides(color = guide_legend(title = "drv")) and so it's not that big of a deal, but it did seem odd to me.

@batpigandme batpigandme added the bug an unexpected problem or unintended behavior label Jan 17, 2018
@clauswilke
Copy link
Member

As far as I can tell, this is a general problem whenever a stat defines a default aesthetic that the user hasn't mapped but does so in a later layer. This is not specific to geom_density2d().

Alternative reprex, with geom_density(), which calls stat_density(), which creates a fill aesthetic:

library(ggplot2)
ggplot(mpg, aes(displ)) +
  geom_density() +
  geom_point(aes(y = .1, fill = drv), shape = 21)

@clauswilke
Copy link
Member

See also:

library(ggplot2)
p <- ggplot(mpg, aes(displ)) +
  geom_density() +
  geom_point(aes(y = .1, fill = drv), shape = 21)
ggplot_build(p)$plot$labels
#> $x
#> [1] "displ"
#> 
#> $y
#> [1] "density"
#> 
#> $fill
#> [1] "fill"

p <- ggplot(mpg, aes(displ)) +
  geom_density()# +
  #geom_point(aes(y = .1, fill = drv), shape = 21)
ggplot_build(p)$plot$labels
#> $x
#> [1] "displ"
#> 
#> $y
#> [1] "density"
#> 
#> $fill
#> [1] "fill"

p <- ggplot(mpg, aes(displ)) +
  #geom_density() +
  geom_point(aes(y = .1, fill = drv), shape = 21)
ggplot_build(p)$plot$labels
#> $x
#> [1] "displ"
#> 
#> $y
#> [1] "y"
#> 
#> $fill
#> [1] "drv"

Note that geom_density() also overwrites the y axis label, and we probably want that to be the case even when we add points with a y aesthetic, so it's not immediately clear what the correct behavior is.

@hadley
Copy link
Member

hadley commented May 28, 2018

Is this a long standing issue, or did it get introduced in 2.3?

@clauswilke
Copy link
Member

clauswilke commented May 28, 2018

Something seems to have changed. Output with ggplot2 2.2.1:

library(ggplot2)
p <- ggplot(mpg, aes(displ)) +
  geom_density() +
  geom_point(aes(y = .1, fill = drv), shape = 21)
ggplot_build(p)$plot$labels
#> $x
#> [1] "displ"
#> 
#> $y
#> [1] "density"
#> 
#> $fill
#> [1] "NA"

Created on 2018-05-28 by the reprex package (v0.2.0).

Session info
devtools::session_info()
#> Session info -------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.5.0 (2018-04-23)
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  tz       America/Chicago             
#>  date     2018-05-28
#> Packages -----------------------------------------------------------------
#>  package    * version    date       source                          
#>  backports    1.1.2      2017-12-13 CRAN (R 3.5.0)                  
#>  base       * 3.5.0      2018-04-24 local                           
#>  colorspace   1.4-0      2017-12-23 R-Forge (R 3.5.0)               
#>  compiler     3.5.0      2018-04-24 local                           
#>  datasets   * 3.5.0      2018-04-24 local                           
#>  devtools     1.13.5     2018-02-18 CRAN (R 3.5.0)                  
#>  digest       0.6.15     2018-01-28 CRAN (R 3.5.0)                  
#>  evaluate     0.10.1     2017-06-24 CRAN (R 3.5.0)                  
#>  ggplot2    * 2.2.1      2016-12-30 CRAN (R 3.5.0)                  
#>  graphics   * 3.5.0      2018-04-24 local                           
#>  grDevices  * 3.5.0      2018-04-24 local                           
#>  grid         3.5.0      2018-04-24 local                           
#>  gtable       0.2.0      2016-02-26 CRAN (R 3.5.0)                  
#>  htmltools    0.3.6      2017-04-28 CRAN (R 3.5.0)                  
#>  knitr        1.20       2018-02-20 CRAN (R 3.5.0)                  
#>  labeling     0.3        2014-08-23 CRAN (R 3.5.0)                  
#>  lazyeval     0.2.1      2017-10-29 CRAN (R 3.5.0)                  
#>  magrittr     1.5        2014-11-22 CRAN (R 3.5.0)                  
#>  memoise      1.1.0      2017-04-21 CRAN (R 3.5.0)                  
#>  methods    * 3.5.0      2018-04-24 local                           
#>  munsell      0.4.3      2016-02-13 CRAN (R 3.5.0)                  
#>  pillar       1.2.1      2018-02-27 CRAN (R 3.5.0)                  
#>  plyr         1.8.4      2016-06-08 CRAN (R 3.5.0)                  
#>  Rcpp         0.12.16    2018-03-13 CRAN (R 3.5.0)                  
#>  rlang        0.2.0.9001 2018-05-15 Github (r-lib/rlang@854174a)    
#>  rmarkdown    1.9        2018-03-01 CRAN (R 3.5.0)                  
#>  rprojroot    1.3-2      2018-01-03 CRAN (R 3.5.0)                  
#>  scales       0.5.0.9000 2018-05-15 Github (hadley/scales@d767915)  
#>  stats      * 3.5.0      2018-04-24 local                           
#>  stringi      1.1.7      2018-03-12 CRAN (R 3.5.0)                  
#>  stringr      1.3.0      2018-02-19 CRAN (R 3.5.0)                  
#>  tibble       1.4.2      2018-01-22 CRAN (R 3.5.0)                  
#>  tools        3.5.0      2018-04-24 local                           
#>  utils      * 3.5.0      2018-04-24 local                           
#>  withr        2.1.2      2018-05-15 Github (jimhester/withr@79d7b0d)
#>  yaml         2.1.18     2018-03-08 CRAN (R 3.5.0)

To highlight the difference, in ggplot2 2.2.1:

#> $fill
#> [1] "NA"

instead of ggplot2 2.3.0:

#> $fill
#> [1] "fill"

@clauswilke
Copy link
Member

However, the resulting 2.2.1 behavior wasn't necessarily better:

library(ggplot2)
ggplot(mpg, aes(displ)) +
  geom_density() +
  geom_point(aes(y = .1, fill = drv), shape = 21)

Created on 2018-05-28 by the reprex package (v0.2.0).

Session info
devtools::session_info()
#> Session info -------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.5.0 (2018-04-23)
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  tz       America/Chicago             
#>  date     2018-05-28
#> Packages -----------------------------------------------------------------
#>  package    * version    date       source                          
#>  backports    1.1.2      2017-12-13 CRAN (R 3.5.0)                  
#>  base       * 3.5.0      2018-04-24 local                           
#>  colorspace   1.4-0      2017-12-23 R-Forge (R 3.5.0)               
#>  compiler     3.5.0      2018-04-24 local                           
#>  curl         3.1        2017-12-12 CRAN (R 3.5.0)                  
#>  datasets   * 3.5.0      2018-04-24 local                           
#>  devtools     1.13.5     2018-02-18 CRAN (R 3.5.0)                  
#>  digest       0.6.15     2018-01-28 CRAN (R 3.5.0)                  
#>  evaluate     0.10.1     2017-06-24 CRAN (R 3.5.0)                  
#>  ggplot2    * 2.2.1      2016-12-30 CRAN (R 3.5.0)                  
#>  graphics   * 3.5.0      2018-04-24 local                           
#>  grDevices  * 3.5.0      2018-04-24 local                           
#>  grid         3.5.0      2018-04-24 local                           
#>  gtable       0.2.0      2016-02-26 CRAN (R 3.5.0)                  
#>  htmltools    0.3.6      2017-04-28 CRAN (R 3.5.0)                  
#>  httr         1.3.1      2017-08-20 CRAN (R 3.5.0)                  
#>  knitr        1.20       2018-02-20 CRAN (R 3.5.0)                  
#>  labeling     0.3        2014-08-23 CRAN (R 3.5.0)                  
#>  lazyeval     0.2.1      2017-10-29 CRAN (R 3.5.0)                  
#>  magrittr     1.5        2014-11-22 CRAN (R 3.5.0)                  
#>  memoise      1.1.0      2017-04-21 CRAN (R 3.5.0)                  
#>  methods    * 3.5.0      2018-04-24 local                           
#>  mime         0.5        2016-07-07 CRAN (R 3.5.0)                  
#>  munsell      0.4.3      2016-02-13 CRAN (R 3.5.0)                  
#>  pillar       1.2.1      2018-02-27 CRAN (R 3.5.0)                  
#>  plyr         1.8.4      2016-06-08 CRAN (R 3.5.0)                  
#>  R6           2.2.2      2017-06-17 CRAN (R 3.5.0)                  
#>  Rcpp         0.12.16    2018-03-13 CRAN (R 3.5.0)                  
#>  rlang        0.2.0.9001 2018-05-15 Github (r-lib/rlang@854174a)    
#>  rmarkdown    1.9        2018-03-01 CRAN (R 3.5.0)                  
#>  rprojroot    1.3-2      2018-01-03 CRAN (R 3.5.0)                  
#>  scales       0.5.0.9000 2018-05-15 Github (hadley/scales@d767915)  
#>  stats      * 3.5.0      2018-04-24 local                           
#>  stringi      1.1.7      2018-03-12 CRAN (R 3.5.0)                  
#>  stringr      1.3.0      2018-02-19 CRAN (R 3.5.0)                  
#>  tibble       1.4.2      2018-01-22 CRAN (R 3.5.0)                  
#>  tools        3.5.0      2018-04-24 local                           
#>  utils      * 3.5.0      2018-04-24 local                           
#>  withr        2.1.2      2018-05-15 Github (jimhester/withr@79d7b0d)
#>  xml2         1.2.0      2018-01-24 CRAN (R 3.5.0)                  
#>  yaml         2.1.18     2018-03-08 CRAN (R 3.5.0)

@hadley
Copy link
Member

hadley commented May 28, 2018

Ah ok, let's consider for the next release then. I think the change is correct, but we need better behaviour when combining labels across multiple layers.

@clauswilke clauswilke changed the title legend title with geom_density_2d Default aesthetics for stats can create unexpected legend titles May 28, 2018
@clauswilke
Copy link
Member

For future reference, this is the code that makes labels for default aesthetics of stats:

mapping <- make_labels(object$mapping)
default <- make_labels(object$stat$default_aes)
new_labels <- defaults(mapping, default)
plot$labels <- defaults(plot$labels, new_labels)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior scales 🐍
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants