Type: Package
Title: Create a Segmented Total Bar Plot with Custom Annotations and Labels
Version: 0.1.0
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.
License: MIT + file LICENSE
URL: https://github.com/ozancanozdemir/ggsegmentedtotalbar
BugReports: https://github.com/ozancanozdemir/ggsegmentedtotalbar/issues
Depends: R (≥ 4.0)
Imports: ggplot2, grid, forcats
Suggests: testthat (≥ 3.0.0), devtools, remotes, roxygen2, usethis, knitr, rmarkdown
Encoding: UTF-8
Config/testthat/edition: 3
RoxygenNote: 7.2.3
VignetteBuilder: knitr
NeedsCompilation: no
Packaged: 2025-04-27 14:18:29 UTC; ozancanozdemir
Author: Ozancan Ozdemir [aut, cre]
Maintainer: Ozancan Ozdemir <ozancanozdemir@gmail.com>
Repository: CRAN
Date/Publication: 2025-04-28 18:40:09 UTC

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",
  label = FALSE,
  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".

label

Logical. If 'TRUE', adds labels showing total values above the boxes and value labels on each segment. Default is 'FALSE'.

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", label = TRUE)