Learn R Programming

ggformula (version 0.7.0)

gf_smooth: Formula interface to geom_smooth()

Description

LOESS and linear model smoothers in ggformula.

Usage

gf_smooth(object = NULL, gformula = NULL, data = NULL, method = "auto",
  formula = y ~ x, se = TRUE, method.args, n = 80, span = 0.75,
  fullrange = FALSE, level = 0.95, xlab, ylab, title, subtitle, caption,
  geom = "smooth", stat = "smooth", position = "identity",
  show.legend = NA, show.help = NULL, inherit = TRUE,
  environment = parent.frame(), ...)

gf_lm(object = NULL, gformula = NULL, data = NULL, alpha = 0.3, lm.args = list(), interval = "none", level = 0.95, fullrange = TRUE, xlab, ylab, title, subtitle, caption, geom = "lm", stat = "lm", position = "identity", show.legend = NA, show.help = NULL, inherit = TRUE, environment = parent.frame(), ...)

Arguments

object

When chaining, this holds an object produced in the earlier portions of the chain. Most users can safely ignore this argument. See details and examples.

gformula

A formula with shape y ~ x. Faceting can be achieved by including | in the formula.

data

A data frame with the variables to be plotted.

method

smoothing method (function) to use, eg. "lm", "glm", "gam", "loess", "rlm".

For method = "auto" the smoothing method is chosen based on the size of the largest group (across all panels). loess is used for than 1,000 observations; otherwise gam is used with formula = y ~ s(x, bs = "cs"). Somewhat anecdotally, loess gives a better appearance, but is O(n^2) in memory, so does not work for larger datasets.

formula

formula to use in smoothing function, eg. y ~ x, y ~ poly(x, 2), y ~ log(x)

se

display confidence interval around smooth? (TRUE by default, see level to control

method.args

List of additional arguments passed on to the modelling function defined by method.

n

number of points to evaluate smoother at

span

Controls the amount of smoothing for the default loess smoother. Smaller numbers produce wigglier lines, larger numbers produce smoother lines.

fullrange

should the fit span the full range of the plot, or just the data

level

level of confidence interval to use (0.95 by default)

xlab

Label for x-axis. See also gf_labs().

ylab

Label for y-axis. See also gf_labs().

title

Title, sub-title, and caption for the plot. See also gf_labs().

subtitle

Title, sub-title, and caption for the plot. See also gf_labs().

caption

Title, sub-title, and caption for the plot. See also gf_labs().

geom

A character string naming the geom used to make the layer.

stat

A character string naming the stat used to make the layer.

position

Either a character string naming the position function used for the layer or a position object returned from a call to a position function.

show.legend

A logical indicating whether this layer should be included in the legends. NA, the default, includes layer in the legends if any of the attributes of the layer are mapped.

show.help

If TRUE, display some minimal help.

inherit

A logical indicating whether default attributes are inherited.

environment

An environment in which to look for variables not found in data.

...

Additional arguments. Typically these are (a) ggplot2 aesthetics to be set with attribute = value, (b) ggplot2 aesthetics to be mapped with attribute = ~expression, or (c) attributes of the layer as a whole, which are set with attribute = value.

alpha

Opacity (0 = invisible, 1 = opaque).

lm.args

A list of arguments to stats::lm().

interval

One of "none", "confidence" or "prediction".

Value

a gg object

Details

Positional aesthetics are specified using the formula in gformula. Setting and mapping of additional attributes can be done through the use of additional arguments. Attributes can be set can be set using arguments of the form attribute = value or mapped using arguments of the form attribute = ~ expression.

In formulas of the form A | B, B will be used to form facets using facet_wrap() or facet_grid(). This provides an alternative to gf_facet_wrap() and gf_facet_grid() that is terser and may feel more familiar to users of lattice.

Evaluation of the ggplot2 code occurs in the environment of gformula. This will typically do the right thing when formulas are created on the fly, but might not be the right thing if formulas created in one environment are used to create plots in another.

See Also

ggplot2::geom_smooth(), gf_spline()

Examples

Run this code
# NOT RUN {
gf_smooth()
gf_lm()
if (require(mosaicData)) {
  gf_smooth(births ~ date, color = ~wday, data = Births78)
  gf_smooth(births ~ date, color = ~wday, data = Births78, fullrange = TRUE)
  gf_smooth(births ~ date, color = ~wday, data = Births78, show.legend = FALSE, se = FALSE)
  gf_lm(length ~ width, data = KidsFeet, color = ~biggerfoot, alpha = 0.2) %>%
    gf_point()
  gf_lm(length ~ width, data = KidsFeet, color = ~biggerfoot, fullrange = FALSE, alpha = 0.2)
    gf_point()
  gf_lm(length ~ width, color = ~ sex, data = KidsFeet,
        formula = y ~ poly(x,2), linetype = "dashed") %>%
    gf_point()
  gf_lm(length ~ width, color = ~ sex, data = KidsFeet,
        formula = log(y) ~ x, backtrans = exp) %>%
    gf_point()
}
gf_lm(hwy ~ displ, data = mpg,
      formula = log(y) ~ poly(x,3), backtrans = exp,
     interval = "prediction", fill = "skyblue") %>%
  gf_lm(
     formula = log(y) ~ poly(x,3), backtrans = exp,
     interval = "confidence", color = "red") %>%
  gf_point()

# }

Run the code above in your browser using DataLab