Skip to contents

For labels drawn inside bars, tiles or other filled shapes: returns ink for a light fill and warm_white for a dark one, whichever has the higher WCAG contrast ratio against that fill. It is vectorized, so it works on a whole column of fills.

Usage

maison_label_color(fill)

Arguments

fill

A vector of colors: hex codes or R color names.

Value

A character vector of hex codes, the same length as fill.

Details

Inside aes(), wrap the call in ggplot2::after_scale() so it sees the fill colors the scale has chosen: aes(colour = after_scale(maison_label_color(fill))). The label layer needs the same fill mapping as the bars, even though text draws no fill; ggplot2 notes that geom_text() ignores it.

A translucent fill is judged as if it were opaque, because the function cannot know what is behind it.

See also

maison_colors for the two label colors.

Examples

maison_label_color(c("#463752", "#DBA940", "#FAF9F7"))
#> [1] "#FAF9F7" "#28282B" "#28282B"
maison_label_color(maison_pal("likert")(5))
#> [1] "#28282B" "#28282B" "#28282B" "#28282B" "#FAF9F7"

library(ggplot2)
df <- data.frame(brand = c("Acme", "Bolt", "Core"), share = c(42, 31, 27))
ggplot(df, aes(share, brand, fill = brand)) +
  geom_col(show.legend = FALSE) +
  geom_text(
    aes(label = share, colour = after_scale(maison_label_color(fill))),
    hjust = 1.4
  ) +
  theme_maison(base_family = "sans", grid = "X")