Skip to content

feat: implement error bars in bar and line charts#104

Open
TymekDev wants to merge 1 commit intoardata-fr:masterfrom
TymekDev:error-bars
Open

feat: implement error bars in bar and line charts#104
TymekDev wants to merge 1 commit intoardata-fr:masterfrom
TymekDev:error-bars

Conversation

@TymekDev
Copy link

@TymekDev TymekDev commented Nov 20, 2025

This patch implements vertical (Y-axis) error bars support for both bar and line charts. The error bars can be added by providing the error_y_lower and/or error_y_upper arguments to the ms_barchart and ms_linechart functions. Those arguments expect a column name present in the provided data with absolute values for the error bars to display at. The new arguments default to NULL which shouldn't change the current function behavior.

For this implementation I considered another approach: using a function to modify the chart object. However, I decided against it, because the underlying ms_chart constructor handles data transformations and writing the underlying Excel sheet. Making the user provide an already-transformed error bars data would be quite cumbersome when working with multiple series.

The way this implementation works is adding a new field to the chart object—error_bar_colnames. This is a list with y field (x support can be added later on) which is either NULL or a list containing either or both lower and upper fields that store the column name provided by the user. Additionally, the data has a new column mutated which is the offset to be used by the error bars (the arguments expect the columns to have absolute values). Those columns related to error bars are also included in y argument during pivoting the data to write to the underlying Excel. Finally, as_series uses the calculated offset columns to create an ms_error_bars object which then gets converted with its to_pml.ms_error_bars implementation during the to_pml conversion (in both bar and line charts).

Additionally, I slightly refactored the as_series function, because it got noisy while being quite repetitive.

Closes #92

Examples

Setup

install.packages(c("dplyr", "remotes"))
remotes::install_github("TymekDev/mschart@error-bars")

chart_to_pptx <- function(x, open = FALSE, output_path = tempfile(fileext = ".pptx")) {
  officer::read_pptx() |>
    officer::add_slide(layout = "Title and Content", master = "Office Theme") |>
    officer::ph_with(value = x, location = officer::ph_location_fullsize()) |>
    print(target = output_path)

  if (open) {
    browseURL(output_path)
  }

  output_path
}

fake_data <- tibble::tribble(
  ~month, ~sales, ~"sales lower", ~"sales_upper", ~"person",
  "Jan",     120,            100,            135,    "Jane",
  "Feb",     150,            130,            165,    "Jane",
  "Mar",     135,            115,            150,    "Jane",
  "Apr",     170,            150,            185,    "Jane",
  "May",     160,            140,            175,    "Jane",
  "Jan",      12,              9,             19,    "John",
  "Feb",      15,             12,             22,    "John",
  "Mar",     135,            132,            142,    "John",
  "Apr",      17,             14,             24,    "John",
  "May",      16,             13,             23,    "John",
)

Single series line chart with lower error bars

fake_data |>
  dplyr::filter(person == "Jane") |>
  mschart::ms_linechart(
    x = "month",
    y = "sales",
    error_y_lower = "sales lower"
  ) |>
  chart_to_pptx(open = TRUE)
CleanShot 2025-11-20 at 15 28 38

Two series line chart with lower and upper error bars

fake_data |>
  mschart::ms_linechart(
    x = "month",
    y = "sales",
    group = "person",
    error_y_lower = "sales lower",
    error_y_upper = "sales_upper"
  ) |>
  chart_to_pptx(open = TRUE)
CleanShot 2025-11-20 at 15 29 08

Two series bar chart with lower and upper error bars

fake_data |>
  mschart::ms_barchart(
    x = "month",
    y = "sales",
    group = "person",
    error_y_lower = "sales lower",
    error_y_upper = "sales_upper"
  ) |>
  chart_to_pptx(open = TRUE)
CleanShot 2025-11-20 at 15 30 52

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error bars on ms_barchart

1 participant