Skip to contents

Add a line to an existing chart (bar, scatter and line types supported). On scatter charts you can also add a smooth line.

Usage

add_line(
  ax,
  mapping,
  data = NULL,
  type = c("line", "spline"),
  serie_name = NULL
)

add_smooth_line(
  ax,
  formula = y ~ x,
  model = c("lm", "loess"),
  n = 100,
  ...,
  type = c("line", "spline"),
  serie_name = NULL
)

Arguments

ax

An apexchart() htmlwidget object.

mapping

Default list of aesthetic mappings to use for chart.

data

A data.frame to use to add a line, if NULL (default), the data.frame provided in apex() will be used.

type

Type of line.

serie_name

Name for the serie displayed in tooltip and legend.

formula

Formula passed to the method, default to y ~ x from main aesthetics.

model

Model to use between lm or loess.

n

Number of points used for predictions.

...

Arguments passed to model.

Value

An apexchart() htmlwidget object.

Examples

library(apexcharter)


# Bar ----

data("climate_paris")

# Add a line on a column's chart
apex(climate_paris, aes(month, precipitation), type = "column") %>% 
  add_line(aes(month, temperature))
# Add secondary axis apex(climate_paris, aes(month, precipitation), type = "column") %>% add_line(aes(month, temperature)) %>% ax_yaxis( title = list(text = "Precipitation (in mm)") ) %>% ax_yaxis2( opposite = TRUE, decimalsInFloat = 0, title = list(text = "Temperature (in degree celsius)") ) %>% ax_dataLabels( enabled = TRUE, enabledOnSeries = list(1) )
# Scatter ---- # add smooth line on scatter plot apex(cars, aes(speed, dist), type = "scatter") %>% add_line(aes(x, y), data = lowess(cars), serie_name = "lowess")
# or directly apex(cars, aes(speed, dist), type = "scatter") %>% add_smooth_line()
apex(cars, aes(speed, dist), type = "scatter") %>% add_smooth_line(model = "loess", span = 1)
apex(cars, aes(speed, dist), type = "scatter") %>% add_smooth_line(model = "loess", degree = 1)
apex(cars, aes(speed, dist), type = "scatter") %>% add_smooth_line(formula = y ~ poly(x, 2))
apex(cars, aes(speed, dist), type = "scatter") %>% add_smooth_line(model = "lm", serie_name = "lm") %>% add_smooth_line(model = "loess", serie_name = "loess")