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.
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 toplotly::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)
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)