Skip to contents

This function creates a bar plot with error bars based on two groups split by a year value. It can optionally create an interactive plot and show individual data points.

Usage

show_splitbarplot(
  df,
  period_col,
  year_col,
  value_col,
  x_label_prefix = "",
  interactive = TRUE,
  source = "A",
  exploded = FALSE,
  bars_fill = c("#00BFC4", "#F8766D"),
  bars_alpha = 0.7,
  label_points = c("min", "max", "median"),
  label_color = "black",
  value_round = 2,
  text_size = 14
)

Arguments

df

A data frame containing the data to plot

period_col

Name of the column containing values "before" and "after" as an ordered factor

year_col

Name of the column containing year values

value_col

Name of the column containing the values to plot

x_label_prefix

Optional prefix for x-axis labels (default: "")

interactive

Logical; if TRUE, creates an interactive plot using plotly (default: TRUE)

source

if interactive is TRUE, the source argument to plotly::ggplotly for referencing with click events (default: "A")

exploded

Logical; if TRUE, adds individual points with labels for min, max, and mean (default: FALSE)

bars_fill

Fill color for bars (default: c("#00BFC4", "#F8766D"); teal and red)

bars_alpha

Alpha transparency for bars (default: 0.7)

label_points

Character vector specifying which points to label in exploded view (default: c("min","max","median"))

label_color

Color for labeled points in exploded view (default: "black")

value_round

Integer indicating number of decimal places for rounding values (default: 2)

text_size

Size of text for labels and hover text (default: 14)

Value

A ggplot2 object or plotly object (if interactive = TRUE)

Examples

# Create example data
data <- data.frame(
  date = seq.Date(as.Date("2010-01-01"), as.Date("2020-12-31"), by = "month"),
  value = c(rnorm(66, mean = 10, sd = 4), rnorm(66, mean = 20, sd = 4))
  )

# Basic analysis with default statistics
split_date <- as.Date("2015-06-15")
df <- anlz_splitdata(data, split_date, "date", "value")

# Basic interactive plot
show_splitbarplot(df, "period", "year", "avg")
# Custom colors and labels with 1 decimal place show_splitbarplot(df, "period", "year", "avg", bars_fill = c("steelblue", "darkorange"), exploded = TRUE, label_points = c("min", "max"), label_color = "darkred", value_round = 1)