Package 'ggsegmentedtotalbar'

Title: Create a Segmented Total Bar Plot with Custom Annotations and Labels
Description: It provides a better alternative for stacked bar plot by creating a segmented total bar plot with custom annotations and labels. It is useful for visualizing the total of a variable and its segments in a single bar, making it easier to compare the segments and their contributions to the total.
Authors: Ozancan Ozdemir [aut, cre]
Maintainer: Ozancan Ozdemir <[email protected]>
License: MIT + file LICENSE
Version: 0.1.0
Built: 2026-06-08 08:33:27 UTC
Source: https://github.com/ozancanozdemir/ggsegmentedtotalbar

Help Index


Create a segmented total bar plot with custom annotations and labels

Description

This function creates a segmented bar plot where each bar represents a group, divided into segments. Additionally, a background box is drawn behind each bar up to the group's total value. Optionally, total values and segment values can be displayed as labels on the plot.

Usage

ggsegmentedtotalbar(
  df,
  group,
  segment,
  value,
  total,
  alpha = 0.3,
  color = "lightgrey",
  value_label = FALSE,
  total_label = value_label,
  value_vjust = -0.3,
  total_vjust = -0.5,
  label_size = 4,
  label_color = "black"
)

Arguments

df

A data frame containing the data to be plotted.

group

A string specifying the column name for the grouping variable.

segment

A string specifying the column name for the segmenting variable (used for fill color).

value

A string specifying the column name for the value variable (used for the bar heights).

total

A string specifying the column name for the total variable (used for determining the background box height).

alpha

A numeric value (between 0 and 1) controlling the transparency of the background boxes. Default is 0.3.

color

A string specifying the color of the background boxes. Default is "lightgrey".

value_label

Logical. If 'TRUE', adds labels showing values on each segment. Default is 'FALSE'.

total_label

Logical. If 'TRUE', adds labels showing total values above the boxes. Default is same as 'value_lavel'.

value_vjust

Numeric. adjust the alignment of values labels. Default is -0.3.

total_vjust

Numeric. adjust the alignment of total values labels. Default is -0.5.

label_size

Numeric. Text size for the labels. Default is 4.

label_color

A string specifying the color of the labels. Default is "black".

Details

The group levels are ordered from the group with the highest total value to the one with the lowest.

Value

A ggplot object displaying the segmented bar plot with optional annotations and labels.

Examples

df_ex <- data.frame(
  group = rep(c("West", "East", "Central", "South"), each = 3),
  segment = rep(c("Consumer", "Corporate", "Home Office"), 4),
  value = c(364, 232, 143, 357, 204, 131, 254, 158, 91, 196, 122, 74),
  total = rep(c(739, 692, 503, 392), each = 3)
)
ggsegmentedtotalbar(df_ex, group = "group", segment = "segment",
                    value = "value", total = "total", value_label = TRUE)