tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.52k stars 2.03k forks source link

geom_smooth() drops variables from plot but not from legend #3363

Closed ptoche closed 1 year ago

ptoche commented 5 years ago

The se fill when variable factor(gear) is equal to 5 is dropped from the plot, but still appears in the legend. I think it should be dropped from the legend too.

``` r
library(reprex)
library(ggplot2)
#> Registered S3 methods overwritten by 'ggplot2':
#>   method         from 
#>   [.quosures     rlang
#>   c.quosures     rlang
#>   print.quosures rlang
ggplot(data = mtcars, aes(x = mpg, y = disp)) +
    geom_smooth(aes(fill = factor(gear))) 
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'
#> Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
#> parametric, : span too small. fewer data values than degrees of freedom.
#> Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
#> parametric, : pseudoinverse used at 14.923
#> Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
#> parametric, : neighborhood radius 4.777
#> Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
#> parametric, : reciprocal condition number 0
#> Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
#> parametric, : There are other near singularities as well. 116.14
#> Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
#> parametric, : Chernobyl! trL>n 5

#> Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
#> parametric, : Chernobyl! trL>n 5
#> Warning in sqrt(sum.squares/one.delta): NaNs produced
#> Warning in predLoess(object$y, object$x, newx = if
#> (is.null(newdata)) object$x else if (is.data.frame(newdata))
#> as.matrix(model.frame(delete.response(terms(object)), : span too small.
#> fewer data values than degrees of freedom.
#> Warning in predLoess(object$y, object$x, newx = if
#> (is.null(newdata)) object$x else if (is.data.frame(newdata))
#> as.matrix(model.frame(delete.response(terms(object)), : pseudoinverse used
#> at 14.923
#> Warning in predLoess(object$y, object$x, newx = if
#> (is.null(newdata)) object$x else if (is.data.frame(newdata))
#> as.matrix(model.frame(delete.response(terms(object)), : neighborhood radius
#> 4.777
#> Warning in predLoess(object$y, object$x, newx = if
#> (is.null(newdata)) object$x else if (is.data.frame(newdata))
#> as.matrix(model.frame(delete.response(terms(object)), : reciprocal
#> condition number 0
#> Warning in predLoess(object$y, object$x, newx = if
#> (is.null(newdata)) object$x else if (is.data.frame(newdata))
#> as.matrix(model.frame(delete.response(terms(object)), : There are other
#> near singularities as well. 116.14
#> Warning in stats::qt(level/2 + 0.5, pred$df): NaNs produced
```

![](https://i.imgur.com/Zmj00Yk.png)

<sup>Created on 2019-06-14 by the [reprex package](https://reprex.tidyverse.org) (v0.3.0)</sup>
ptoche commented 5 years ago

Plot not showing above for some reason, so here:

paleolimbot commented 5 years ago

The legend does not use the layer data, only the scales and the draw_key from the geometry. I think that changing the code to enable this would probably be a lot of work.

teunbrand commented 1 year ago

I think discussion about smarter guide/layer interactions should be continued in #3648.

However, geom_smooth() doesn't drop that fill level at all. It just sets the ymin/ymax aesthetics to NA because the standard error couldn't be calculated. So even if we would get guides to be smarter, it is still unlikely that this case can be detected. I don't think it is currently worth it to special-case such situation.