Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 125 lines (117 sloc) 4.162 kB
953fe47 @hadley Convert all documentation to roxygen
authored
1 #' Add a smoothed conditional mean.
2 #'
7e4a37e @hadley Combine geom_smooth and stat_smooth docs. #1186
authored
3 #' Aids the eye in seeing patterns in the presence of overplotting.
4 #' \code{geom_smooth} and \code{stat_smooth} are effectively aliases: they
5 #' both use the same arguments. Use \code{geom_smooth} unless you want to
6 #' display the results with a non-standard geom.
7 #'
8 #' Calculation is performed by the (currently undocumented)
9 #' \code{predictdf} generic and its methods. For most methods the standard
10 #' error bounds are computed using the \code{\link{predict}} method - the
11 #' exceptions are \code{loess} which uses a t-based approximation, and
12 #' \code{glm} where the normal confidence interval is constructed on the link
13 #' scale, and then back-transformed to the response scale.
14 #'
1244716 @hadley Trim whitespace
authored
15 #' @section Aesthetics:
d7971cb @hadley Fix document aesthetics.
authored
16 #' \Sexpr[results=rd,stage=build]{ggplot2:::rd_aesthetics("geom", "smooth")}
17 #'
7b5d0a7 @hadley Inherit geom parameters
authored
18 #' @inheritParams geom_point
7e4a37e @hadley Combine geom_smooth and stat_smooth docs. #1186
authored
19 #' @param geom,stat Use to override the default connection between
20 #' \code{geom_smooth} and \code{stat_smooth}.
21 #' @seealso See individual modelling functions for more details:
22 #' \code{\link{lm}} for linear smooths,
23 #' \code{\link{glm}} for generalised linear smooths,
24 #' \code{\link{loess}} for local smooths
953fe47 @hadley Convert all documentation to roxygen
authored
25 #' @export
26 #' @examples
7e4a37e @hadley Combine geom_smooth and stat_smooth docs. #1186
authored
27 #' ggplot(mpg, aes(displ, hwy)) +
ef877fe @hadley geom_smooth tweaks: bump line size & improve docs
authored
28 #' geom_point() +
29 #' geom_smooth()
1244716 @hadley Trim whitespace
authored
30 #'
7e4a37e @hadley Combine geom_smooth and stat_smooth docs. #1186
authored
31 #' # Use span to control the "wiggliness" of the default loess smoother
32 #' # The span is the fraction of points used to fit each local regression:
33 #' # small numbers make a wigglier curve, larger numbers make a smoother curve.
34 #' ggplot(mpg, aes(displ, hwy)) +
35 #' geom_point() +
36 #' geom_smooth(span = 0.3)
37 #'
38 #' # Instead of a loess smooth, you can use any other modelling function:
39 #' ggplot(mpg, aes(displ, hwy)) +
ef877fe @hadley geom_smooth tweaks: bump line size & improve docs
authored
40 #' geom_point() +
41 #' geom_smooth(method = "lm", se = FALSE)
42 #'
7e4a37e @hadley Combine geom_smooth and stat_smooth docs. #1186
authored
43 #' ggplot(mpg, aes(displ, hwy)) +
44 #' geom_point() +
45 #' geom_smooth(method = "lm", formula = y ~ splines::bs(x, 3), se = FALSE)
ef877fe @hadley geom_smooth tweaks: bump line size & improve docs
authored
46 #'
7e4a37e @hadley Combine geom_smooth and stat_smooth docs. #1186
authored
47 #' # Smoothes are automatically fit to each group (defined by categorical
48 #' # aesthetics or the group aesthetic) and for each facet
ef877fe @hadley geom_smooth tweaks: bump line size & improve docs
authored
49 #'
7e4a37e @hadley Combine geom_smooth and stat_smooth docs. #1186
authored
50 #' ggplot(mpg, aes(displ, hwy, colour = class)) +
ef877fe @hadley geom_smooth tweaks: bump line size & improve docs
authored
51 #' geom_point() +
7e4a37e @hadley Combine geom_smooth and stat_smooth docs. #1186
authored
52 #' geom_smooth(se = FALSE, method = "lm")
53 #' ggplot(mpg, aes(displ, hwy)) +
54 #' geom_point() +
55 #' geom_smooth(span = 0.8) +
56 #' facet_wrap(~drv)
ef877fe @hadley geom_smooth tweaks: bump line size & improve docs
authored
57 #'
7e4a37e @hadley Combine geom_smooth and stat_smooth docs. #1186
authored
58 #' \dontrun{
59 #' # To fit a logistic regression, you need to coerce the values to
60 #' # a numeric vector lying between 0 and 1.
23e95e1 @hadley Eliminate use of safe.call. Fixes #1245
authored
61 #' binomial_smooth <- function(...) {
62 #' geom_smooth(method = "glm", method.args = list(family = "binomial"), ...)
63 #' }
64 #'
7e4a37e @hadley Combine geom_smooth and stat_smooth docs. #1186
authored
65 #' ggplot(rpart::kyphosis, aes(Age, Kyphosis)) +
66 #' geom_jitter(height = 0.05) +
23e95e1 @hadley Eliminate use of safe.call. Fixes #1245
authored
67 #' binomial_smooth()
1244716 @hadley Trim whitespace
authored
68 #'
7e4a37e @hadley Combine geom_smooth and stat_smooth docs. #1186
authored
69 #' ggplot(rpart::kyphosis, aes(Age, as.numeric(Kyphosis) - 1)) +
70 #' geom_jitter(height = 0.05) +
23e95e1 @hadley Eliminate use of safe.call. Fixes #1245
authored
71 #' binomial_smooth()
1244716 @hadley Trim whitespace
authored
72 #'
7e4a37e @hadley Combine geom_smooth and stat_smooth docs. #1186
authored
73 #' ggplot(rpart::kyphosis, aes(Age, as.numeric(Kyphosis) - 1)) +
74 #' geom_jitter(height = 0.05) +
23e95e1 @hadley Eliminate use of safe.call. Fixes #1245
authored
75 #' binomial_smooth(formula = y ~ splines::ns(x, 2))
1244716 @hadley Trim whitespace
authored
76 #'
7e4a37e @hadley Combine geom_smooth and stat_smooth docs. #1186
authored
77 #' # But in this case, it's probably better to fit the model yourself
78 #' # so you can exercise more control and see whether or not it's a good model
79 #' }
e3659b5 @hadley Add formula, method & se args back to geom_smooth
authored
80 geom_smooth <- function(mapping = NULL, data = NULL, stat = "smooth",
a273159 @hadley Rename show_guide to show.legend
authored
81 method = "auto", formula = y ~ x, se = TRUE, position = "identity", show.legend = NA,
7e4a37e @hadley Combine geom_smooth and stat_smooth docs. #1186
authored
82 inherit.aes = TRUE, ...) {
e3659b5 @hadley Add formula, method & se args back to geom_smooth
authored
83
138ab04 @wch Change Layer$new() calls to layer(). Closes #1180
wch authored
84 layer(
915c345 @wch Convert all remaining stats and geoms to R6
wch authored
85 data = data,
86 mapping = mapping,
87 stat = stat,
88 geom = GeomSmooth,
89 position = position,
a273159 @hadley Rename show_guide to show.legend
authored
90 show.legend = show.legend,
915c345 @wch Convert all remaining stats and geoms to R6
wch authored
91 inherit.aes = inherit.aes,
bdd605b @hadley Just use single params argument to layer()
authored
92 params = list(
93 method = method,
94 formula = formula,
95 se = se,
96 ...
97 )
915c345 @wch Convert all remaining stats and geoms to R6
wch authored
98 )
ede677c @JakeRuss Removed xxx.r (build_accessor file) and @name tags, added functions t…
JakeRuss authored
99 }
100
4c3d5e2 @wch Document and export Geoms, Stats, and Positions. Closes #1184
wch authored
101 #' @rdname ggplot2-ggproto
102 #' @format NULL
103 #' @usage NULL
104 #' @export
ee96a37 @wch Rename proto2 to ggproto
wch authored
105 GeomSmooth <- ggproto("GeomSmooth", Geom,
edaf93a @hadley Eliminate use of ... in geom drawing functions.
authored
106 draw_group = function(data, panel_scales, coord) {
4a89836 @hadley Simplify proto2 object creation
authored
107 ribbon <- transform(data, colour = NA)
108 path <- transform(data, alpha = NA)
1244716 @hadley Trim whitespace
authored
109
4a89836 @hadley Simplify proto2 object creation
authored
110 has_ribbon <- !is.null(data$ymax) && !is.null(data$ymin)
1244716 @hadley Trim whitespace
authored
111
4a89836 @hadley Simplify proto2 object creation
authored
112 gList(
edaf93a @hadley Eliminate use of ... in geom drawing functions.
authored
113 if (has_ribbon) GeomRibbon$draw_group(ribbon, panel_scales, coord),
114 GeomLine$draw_panel(path, panel_scales, coord)
4a89836 @hadley Simplify proto2 object creation
authored
115 )
116 },
6198457 @hadley Add ggplot
authored
117
f99947f @hadley Rename guide_geom to draw_key #1182
authored
118 draw_key = draw_key_smooth,
1244716 @hadley Trim whitespace
authored
119
4a89836 @hadley Simplify proto2 object creation
authored
120 required_aes = c("x", "y"),
483779b @hadley Ensure all geoms have correct legend type. Add set_scale_default fun…
authored
121
4a89836 @hadley Simplify proto2 object creation
authored
122 default_aes = aes(colour = "#3366FF", fill = "grey60", size = 1,
c9c6fb1 @wch Move legend drawing functions out of Geom objects
wch authored
123 linetype = 1, weight = 1, alpha = 0.4)
915c345 @wch Convert all remaining stats and geoms to R6
wch authored
124 )
Something went wrong with that request. Please try again.