ggvis (version 0.4.5)

layer_model_predictions: Overlay model predictions or a smooth curve.

Description

layer_model_predictions fits a model to the data and draw it with layer_paths and, optionally, layer_ribbons. layer_smooths is a special case of layering model predictions where the model is a smooth loess curve whose smoothness is controlled by the span parameter.

Usage

layer_model_predictions(vis, ..., model, formula = NULL,
  model_args = NULL, se = FALSE, domain = NULL)

layer_smooths(vis, ..., span = 0.75, se = FALSE)

Arguments

vis

Visualisation to modify

...

Visual properties. Stroke properties control only affect line, fill properties only affect standard error band.

model

Name of the model as a string, e.g. "loess", "lm", or "MASS::rlm". Must be the name of a function that produces a standard model object with a predict method. For layer_smooth this is always "loess".

formula

Model formula. If not supplied, guessed from the visual properties, constructing y ~ x.

model_args

A list of additional arguments passed on to the model function.

se

Also display a point-wise standard error band? Defaults to FALSE because interpretation is non-trivial.

domain

If NULL (the default), the domain of the predicted values will be the same as the domain of the prediction variable in the data. It can also be a two-element numeric vector specifying the min and max.

span

For layer_smooth, the span of the loess smoother.

Examples

Run this code
# NOT RUN {
mtcars %>% ggvis(~wt, ~mpg) %>% layer_smooths()
mtcars %>% ggvis(~wt, ~mpg) %>% layer_smooths(se = TRUE)

# Use group by to display multiple smoothes
mtcars %>% ggvis(~wt, ~mpg) %>% group_by(cyl) %>% layer_smooths()

# Control appearance with props
mtcars %>% ggvis(~wt, ~mpg) %>%
  layer_smooths(se = TRUE, stroke := "red", fill := "red", strokeWidth := 5)

# Control the wiggliness with span. Default is 0.75
mtcars %>% ggvis(~wt, ~mpg) %>% layer_points() %>%
  layer_smooths(span = 0.2)
mtcars %>% ggvis(~wt, ~mpg) %>% layer_points() %>%
  layer_smooths(span = 1)
# Map to an input to modify interactively
mtcars %>% ggvis(~wt, ~mpg) %>% layer_points() %>%
  layer_smooths(span = input_slider(0.2, 1))

# Use other modelling functions with layer_model_predictions
mtcars %>% ggvis(~wt, ~mpg) %>%
  layer_points() %>%
  layer_model_predictions(model = "lm") %>%
  layer_model_predictions(model = "MASS::rlm", stroke := "red")

# Custom domain for predictions
mtcars %>% ggvis(~wt, ~mpg) %>% layer_points() %>%
  layer_model_predictions(model = "lm", domain = c(0, 8))
mtcars %>% ggvis(~wt, ~mpg) %>% layer_points() %>%
  layer_model_predictions(model = "lm",
    domain = input_slider(0, 10, value = c(1, 4)))

# layer_smooths() is just compute_smooth() + layer_paths()
# Run loess or other model outside of a visualisation to see what variables
# you get.
mtcars %>% compute_smooth(mpg ~ wt)
mtcars %>% compute_model_prediction(mpg ~ wt, model = "lm")

mtcars %>%
  ggvis(~wt, ~mpg) %>%
  layer_points() %>%
  compute_smooth(mpg ~ wt) %>%
  layer_paths(~pred_, ~resp_, strokeWidth := 2)
# }

Run the code above in your browser using DataLab