View source: R/ipea_contrast_color.R
| ipea_contrast_color | R Documentation |
Given one or more fill/background colours, returns "white"
or "black", whichever gives better contrast against that background.
Useful for value labels drawn inside bars or over points/lines, matching
the Ipea editorial guidelines for graphics, which alternate the colour of
data labels according to how dark the colour behind them is. See
https://www.ipea.gov.br/manualeditorial/padroes/padroes-grafico-visuais/ilustracoes-pi.html.
ipea_contrast_color(background)
background |
A character vector of colours (hex codes or R colour names) used as the fill/background behind the label. |
A character vector the same length as background, containing
"white" or "black".
Other ggplot2 theme functions:
ipea_label_size(),
scale_color_ipea(),
scale_fill_ipea(),
theme_ipea()
ipea_contrast_color(c("#29112F", "#D2A0C9"))
library(ggplot2)
df <- data.frame(grupo = c("A", "B"), valor = c(22.6, 4.7),
cor = c("#29112F", "#D2A0C9"))
ggplot(df, aes(grupo, valor, fill = cor, label = valor)) +
geom_col() +
geom_text(aes(color = ipea_contrast_color(cor)), size = ipea_label_size(),
fontface = "bold") +
scale_fill_identity() +
scale_color_identity() +
theme_ipea()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.