ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

Graphical parameters for `geom_smooth()` band

Open teunbrand opened this issue 3 months ago • 5 comments

This PR aims to fix #6551.

Briefly, we allow setting the colour/linetype/linewidth of the confidence bands in geom_smooth(). A demonstration:

devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2

ggplot(mpg, aes(displ, hwy, fill = drv)) +
  geom_smooth(
    band.colour = "black",
    band.linetype = 3,
    band.linewidth = 0.5
  )
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

Created on 2025-09-26 with reprex v2.1.1

teunbrand avatar Sep 26 '25 08:09 teunbrand

That's right. Do you think missing parameters should be drawn from the central line?

teunbrand avatar Dec 03 '25 14:12 teunbrand

That is how we do it for the others, right? NULL means don't draw, NA means inherit..?

thomasp85 avatar Dec 03 '25 19:12 thomasp85

I think the meaning of NULL and NA are swapped. The main reason I initially didn't take properties from the central line was because it should be hidden by default. Of course we might achieve that by setting transparent colours, 0 linewidth or blank linetypes.

teunbrand avatar Dec 03 '25 19:12 teunbrand

oh, yeah you are right... Then just default the arguments to NA to ensure default is not to draw, and let NULL mean inherit

thomasp85 avatar Dec 04 '25 08:12 thomasp85

I'm now hiding it with band.linetype = "blank". Activating the band lines requires setting a non-blank linetype.

devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2

ggplot(mpg, aes(displ, hwy)) +
  geom_smooth(band.linetype = 1, linetype = 2)
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

band.linetype = NULL inherits from center line.

ggplot(mpg, aes(displ, hwy)) +
  geom_smooth(band.linetype = NULL, linetype = 2)
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

Created on 2025-12-04 with reprex v2.1.1

teunbrand avatar Dec 04 '25 08:12 teunbrand