Skip to contents

Adds a horizontal reference (target) line at a constant value to a bar or line chart. This is a common request for showing a budget, target, threshold or average on top of the plotted series.

The line is drawn natively by adding a constant-value line series that shares the chart's category and value axes (the standard 'Microsoft Office' technique for a target line), so it remains a real, editable chart element rather than a drawn shape. Call the function several times to add more than one reference line.

Usage

chart_reference_line(
  x,
  value,
  label = NULL,
  colour = "red",
  color = colour,
  line_style = c("dashed", "solid", "dotted"),
  line_width = 1.25
)

Arguments

x

an ms_chart object created with ms_barchart() or ms_linechart().

value

a single numeric value giving the position of the horizontal line on the value axis.

label

optional label for the line, shown in the legend. When NULL (default), the numeric value is used.

colour, color

line colour, a single colour name or hex string.

line_style

line dash style, one of "solid", "dashed" or "dotted".

line_width

line width in points.

Value

An updated ms_chart object.

Examples

dat <- data.frame(
  name = c("A", "B", "C", "D", "E"),
  value = c(3, 8, 5, 9, 4),
  stringsAsFactors = FALSE
)

bc <- ms_barchart(data = dat, x = "name", y = "value")
bc <- chart_reference_line(bc, value = 6, label = "Target")