l_layer_smooth: Layer a smooth line for 'loon'

View source: R/l_layer_smooth.R

l_layer_smoothR Documentation

Layer a smooth line for loon

Description

Display a smooth line layer

Usage

l_layer_smooth(
  widget,
  x = NULL,
  y = NULL,
  method = "loess",
  group = "",
  formula = y ~ x,
  interval = c("none", "confidence", "prediction"),
  n = 80,
  span = 0.75,
  level = 0.95,
  methodArgs = list(),
  linecolor = "steelblue",
  linewidth = 2,
  linedash = "",
  confidenceIntervalArgs = list(linecolor = "gray80", linewidth = 4, linedash = ""),
  predictionIntervalArgs = list(linecolor = "gray50", linewidth = 3, linedash = 1),
  label = "smooth",
  parent = "root",
  index = 0,
  ...
)

Arguments

widget

widget path name as a string

x

The x coordinates of line. If it is not provided, x will be inherited from widget

y

The y coordinates of line. If it is not provided, y will be inherited from widget

method

Smoothing method (function) to use, accepts either a character vector, e.g. "lm", "glm", "loess" or a function, e.g. MASS::rlm or mgcv::gam, stats::lm, or stats::loess.

group

Data can be grouped by n dimensional aesthetics attributes, e.g. "color", "size". In addition, any length n vector or data.frame is accommodated.

formula

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

interval

type of interval, could be "none", "confidence" or "prediction" (not for glm)

n

Number of points at which to evaluate smoother.

span

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

level

Level of confidence interval to use (0.95 by default).

methodArgs

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

linecolor

fitted line color.

linewidth

fitted line width

linedash

fitted line dash

confidenceIntervalArgs

the line color, width and dash for confidence interval

predictionIntervalArgs

the line color, width and dash for prediction interval

label

label used in the layers inspector

parent

group layer

index

index of the newly added layer in its parent group

...

additional state initialization arguments, see l_info_states

Examples

if(interactive()) {
# loess fit
p <- l_plot(iris, color = iris$Species)
l1 <- l_layer_smooth(p, interval = "confidence")
l_layer_hide(l1)

# the fits are grouped by points color
l2 <- l_layer_smooth(p, group = "color",
                     method = "lm")

# so far, all intervals are hidden
ls <- l_layer_getChildren(l2)
intervals <- l_layer_getChildren(l_create_handle(c(p,ls[3])))
ci <- l_create_handle(c(p,intervals[3]))
l_layer_show(ci)
# show prediction interval
pi <- l_create_handle(c(p,intervals[2]))
l_layer_show(pi)
# hide all
l_layer_hide(l2)

# Draw a fitted line based on a new data set
shortSepalLength <- (iris$Sepal.Length < 5)
l3 <- l_layer_smooth(p,
                     x = iris$Sepal.Length[shortSepalLength],
                     y = iris$Sepal.Width[shortSepalLength],
                     method = "lm",
                     linecolor = "firebrick",
                     interval = "prediction")
l_layer_hide(l3)

if(require(mgcv)) {
  # a full tensor product smooth
  ## linecolor is the same with the points color
  l4 <- l_layer_smooth(p,
                       method = "gam",
                       formula = y~te(x))
  l_layer_hide(l4)
}

# facets
fp <- l_facet(p, by = iris$Species, inheritLayers = FALSE)
l5 <- l_layer_smooth(fp, method = "lm")

# generalized linear model
if(require("loon.data")) {
  data("SAheart")
  # logit regression
  chd <- as.numeric(SAheart$chd) - 1
  age <- SAheart$age
  p1 <- l_plot(age, chd,
               title = "logit regression")
  gl1 <- l_layer_smooth(p1,
                        method = "glm",
                        methodArgs = list(family = binomial()),
                        interval = "conf")

  # log linear regression
  counts <- c(18,17,15,20,10,20,25,13,12)
  age <- c(40,35,53,46,20,33,48,25,23)
  p2 <- l_plot(age, counts,
               title = "log-linear regression")
  gl2 <- l_layer_smooth(p2,
                        method = "glm",
                        methodArgs = list(family = poisson()),
                        interval = "conf")
}
}

loon documentation built on July 9, 2023, 5:48 p.m.