ruben baetens
ruben baetens

Reputation: 2926

Miscellaneous range of stat_smooth()

The ggplot2-function stat_smooth() has the option fullrange=TRUE or FALSE which decides if the fit is drawn over the range of the data or the range of the graph.

Is there a way to give the stat_smooth() a miscellaneous range ? E.g. if my plot has x in (0,100) but the fitted data x in (40,60), to plot the smoothed fit for range x in (30,70).

Upvotes: 4

Views: 1326

Answers (1)

Rentrop
Rentrop

Reputation: 21497

Use xseq in your stat_smooth call as follows:

stat_smooth(xseq = seq(30,70, length=80))

@ Hadley: Why are xseq, and n as plotting-Parameters not documented in ?geom_smooth? See here for the source-code: https://github.com/hadley/ggplot2/blob/master/R/stat-smooth.r

Example: (Addapted from ?geom_smooth)

ggplot(mtcars, aes(qsec, wt)) + 
  geom_point() + 
  xlim(c(10,30)) + 
  stat_smooth(method=lm, fullrange = TRUE, xseq = seq(11,25, length=80))

Result:

enter image description here

Upvotes: 4

Related Questions