Skip to contents

There is no native or straightforward way to add different background colors to facet stripes in ggplot objects. This wrapper allows you to pass a ggplot object and a number of colors equal to the number of stripes and as a result it returns a plot with colored stripe backgrounds.

Usage

g6tr_stripes_bg(plot, bg_colors = c("#36c8ef", "#50bd90", "#dddddd"))

Arguments

plot

Object of class ggplot with facets.

bg_colors

Character vector of color names/hex values to be used as stripes background colors. Default values are Gradient's blue, green and light grey ("#36c8ef", "#50bd90", "#dddddd").

Value

A ggplot object that can be manipulated further.

Examples

library("dplyr")
library("ggplot2")
df_test <- data.frame(
  gr = rep(c("a", "b", "c"), each = 3),
  fl = rep(c("Yes", "No", "Unsure"), times = 3),
  n = sample.int(n = 5, size = 9, replace = TRUE)
)

plot <- df_test %>%
  ggplot(aes(x = fl, y = n, fill = fl)) +
  geom_bar(stat = "identity") +
  facet_wrap(~ gr, nrow = 1)

new_plot <- g6tr_stripes_bg(plot)
new_plot