Skip to contents

Create an Smooth Line Chart

Usage

v_smooth(
  vc,
  mapping = NULL,
  data = NULL,
  name = NULL,
  method = NULL,
  formula = NULL,
  se = TRUE,
  n = 80,
  span = 0.75,
  ...,
  args_area = NULL,
  serie_id = NULL,
  data_id = NULL
)

Arguments

vc

A chart initialized with vchart().

mapping

Default list of aesthetic mappings to use for chart.

data

Default dataset to use for chart. If not already a data.frame, it will be coerced to with as.data.frame.

name

Name for the serie, only used for single serie (no color/fill aesthetic supplied).

method

Smoothing method (function) to use, accepts either NULL or a character vector, e.g. "lm", "glm", "gam", "loess" or a function, e.g. MASS::rlm or mgcv::gam, stats::lm, or stats::loess. "auto" is also accepted for backwards compatibility. It is equivalent to NULL.

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

If you have fewer than 1,000 observations but want to use the same gam() model that method = NULL would use, then set method = "gam", formula = y ~ s(x, bs = "cs").

formula

Formula to use in smoothing function, eg. y ~ x, y ~ poly(x, 2), y ~ log(x). NULL by default, in which case method = NULL implies formula = y ~ x when there are fewer than 1,000 observations and formula = y ~ s(x, bs = "cs") otherwise.

se

Display confidence interval around smooth? (TRUE by default, see level to control.)

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. Only used with loess, i.e. when method = "loess", or when method = NULL (the default) and there are fewer than 1,000 observations.

...

Additional parameters for lines.

args_area

Arguments for area.

data_id, serie_id

ID for the data/serie, can be used to further customize the chart with v_specs().

Value

A vchart() htmlwidget object.

Examples


library(vchartr)

data("mpg", package =  "ggplot2")

vchart(mpg, aes(displ, hwy)) %>%
  v_smooth()
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
vchart(mpg, aes(displ, hwy)) %>% v_smooth(se = FALSE) #> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
vchart(mpg, aes(displ, hwy, color = class)) %>% v_smooth() #> `geom_smooth()` using method = 'loess' and formula = 'y ~ x' #> Warning: span too small. fewer data values than degrees of freedom. #> Warning: pseudoinverse used at 5.6935 #> Warning: neighborhood radius 0.5065 #> Warning: reciprocal condition number 0 #> Warning: There are other near singularities as well. 0.65044 #> Warning: span too small. fewer data values than degrees of freedom. #> Warning: pseudoinverse used at 5.6935 #> Warning: neighborhood radius 0.5065 #> Warning: reciprocal condition number 0 #> Warning: There are other near singularities as well. 0.65044 #> Warning: pseudoinverse used at 4.008 #> Warning: neighborhood radius 0.708 #> Warning: reciprocal condition number 0 #> Warning: There are other near singularities as well. 0.25 #> Warning: pseudoinverse used at 4.008 #> Warning: neighborhood radius 0.708 #> Warning: reciprocal condition number 0 #> Warning: There are other near singularities as well. 0.25