ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

Better documentation of `xseq` for `stat_smooth()`

Open larmarange opened this issue 2 years ago • 6 comments

The xseq argument is a sort of hidden option for stat_smooth() allowing to select where to draw the smooth line.

Would it be relevant to mention it in the documentation and eventually to provide an example?

larmarange avatar Mar 23 '23 17:03 larmarange

I'm not sure if it is hidden on purpose or not, but typically such options aren't advertised to users because they're considered internal.

teunbrand avatar Mar 23 '23 17:03 teunbrand

Given it's presence in compute_group it is exposed to users, whether we document it or not... My guess is that it has been omitted by an error. I doubt @Hadley can remember either way...

We should in general document all arguments available to users so the best way forward is to lift the argument up into the constructor and document it

thomasp85 avatar Mar 24 '23 09:03 thomasp85

It's pretty esoteric, so I think we should only bother to document it if it's really required.

hadley avatar Mar 24 '23 12:03 hadley

It is more flexible that fullrange and quite useful to extend a line only to the left to show the intercept.

Currently fullrange = TRUE extend to both side. There is no option fullrange = "right" or fullrange = "left" but it could easily be done through xseq.

See an old discussion like https://stackoverflow.com/questions/26705554/extend-geom-smooth-in-a-single-direction

larmarange avatar Mar 24 '23 17:03 larmarange

If folks need that capability, these days I'd recommend that they fit the model themselves.

hadley avatar Mar 24 '23 18:03 hadley

@teunbrand gave me a heads up about this issue, so dropping in another possible (bespoke?) pedagogical xseq use, of adding fitted values of the observed values of x as points along smooth line. And also segments between these fitted values and raw data, giving residuals:

mtcars |> 
  ggplot(aes(wt, mpg)) +
  geom_point() +
  geom_smooth() + 
  stat_smooth(geom = "point",                  # draw fitted values as points
                        color = "blue",                   
                        xseq = mtcars$wt) +
  stat_smooth(geom = "segment",            # draw residuals
                         color = "darkred",       
                         xseq = mtcars$wt,
                         xend = mtcars$wt,
                         yend = mtcars$mpg)

EvaMaeRey avatar Oct 06 '23 18:10 EvaMaeRey