tidyverse / ggplot2

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

`layer` does not work as expected for creating a `geom_smooth` plot #5572

Closed davidhodge931 closed 5 months ago

davidhodge931 commented 11 months ago
library(tidyverse)
library(palmerpenguins)

penguins |>
  ggplot(aes(x = flipper_length_mm,
             y = body_mass_g,)) +
  geom_smooth()
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
#> Warning: Removed 2 rows containing non-finite outside the scale range
#> (`stat_smooth()`).


penguins |>
  ggplot(aes(x = flipper_length_mm,
             y = body_mass_g,)) +
layer(
  geom = ggplot2::GeomSmooth,
  stat = "smooth",
  position = ggplot2::position_identity()
)
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
#> Warning: Removed 2 rows containing non-finite outside the scale range
#> (`stat_smooth()`).

Created on 2023-12-08 with reprex v2.0.2

teunbrand commented 11 months ago

It needs the parameter se = TRUE to show the standard error, i.e.:

penguins |>
  ggplot(aes(x = flipper_length_mm,
             y = body_mass_g,)) +
  layer(
    geom = ggplot2::GeomSmooth,
    stat = "smooth",
    position = ggplot2::position_identity(),
    params = list(se = TRUE)
  )
davidhodge931 commented 11 months ago

Is it possible that GeomSmooth could default to se = TRUE to match what we see in geom_smooth?

teunbrand commented 10 months ago

I'm not sure that would be wise, as I think there is the possibility that geom_smooth() may be uses with a stat that isn't StatSmooth and doesn't produce the ymin/ymax and xmin/ymax variables. However, I see no reason why the default couldn't be NULL and gets resolved based on whether those columns are present in the data.