-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Comments
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 Alternative reprex, with library(ggplot2)
ggplot(mpg, aes(displ)) +
geom_density() +
geom_point(aes(y = .1, fill = drv), shape = 21) |
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 |
Is this a long standing issue, or did it get introduced in 2.3? |
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 infodevtools::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" |
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 infodevtools::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) |
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. |
geom_density_2d
For future reference, this is the code that makes labels for default aesthetics of stats: Lines 159 to 162 in 86c41ac
|
I've run into some unexpected behavior when using
geom_density_2d
.If I run
The legend title corresponds to the variable name (drv).

But if I add a
geom_density_2d
layer that I want plotted under the smooths, the legend title changes to "colour".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.The text was updated successfully, but these errors were encountered: