Learn R Programming

ggblanket (version 7.0.0)

aes_contrast: A colour aesthetic that automatically contrasts with fill.

Description

A colour aesthetic for annotation that automatically contrasts with fill. Can be spliced into ggplot2::aes with rlang::!!!.

Usage

aes_contrast(contrast_pal = lightness[2:3])

Value

An aesthetic

Arguments

contrast_pal

A vector of a dark colour and then a light colour (e.g. greyness[2:3] or darkness[1:2]). Defaults to lightness[2:3].

Examples

Run this code
library(ggplot2)
library(dplyr)
library(stringr)
library(palmerpenguins)

set_blanket()

penguins |>
  count(species, sex) |>
  gg_col(
    x = sex,
    y = n,
    col = species,
    position = position_dodge2(preserve = "single"),
    width = 0.75,
    x_labels = \(x) str_to_sentence(x),
  ) +
  geom_text(
    mapping = aes(label = n, !!!aes_contrast()),
    position = position_dodge2(width = 0.75, preserve = "single"),
    vjust = 1.33,
    show.legend = FALSE,
  )

penguins |>
  count(species, sex) |>
  gg_col(
    x = n,
    y = sex,
    col = species,
    position = position_dodge2(preserve = "single"),
    width = 0.75,
    y_labels = \(x) str_to_sentence(x),
    mode = dark_mode_r(),
  ) +
  geom_text(
    mapping = aes(label = n, !!!aes_contrast(darkness[1:2])),
    position = position_dodge2(width = 0.75, preserve = "single"),
    hjust = 1.25,
    show.legend = FALSE,
  )

Run the code above in your browser using DataLab