Skip to content

Commit

Permalink
Merge pull request #76 from larmarange/auto_contrast
Browse files Browse the repository at this point in the history
auto_contrast
  • Loading branch information
larmarange authored Oct 11, 2024
2 parents 44a1d88 + b74e2b5 commit 2dcf6ca
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 16 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export(StatCross)
export(StatProp)
export(StatWeightedMean)
export(augment_chisq_add_phi)
export(auto_contrast)
export(compute_cascade)
export(geom_diverging)
export(geom_diverging_text)
Expand Down Expand Up @@ -61,3 +62,4 @@ importFrom(lifecycle,deprecated)
importFrom(scales,percent)
importFrom(stats,weighted.mean)
importFrom(stats,weights)
importFrom(utils,modifyList)
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
`geom_diverging_text()`, `geom_likert_text()`, and `geom_pyramid_text()` have
been redesigned (#73)

**Improvements**

* New shortcut `auto_contrast` (#75)

**Other changes**

* Requiring only R minimum version 4.1.0 (#72)
Expand Down
6 changes: 3 additions & 3 deletions R/geom_diverging.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ geom_pyramid <- function(mapping = NULL,

#' @rdname geom_diverging
#' @export
geom_diverging_text <- function(mapping = NULL,
geom_diverging_text <- function(mapping = ggplot2::aes(!!!auto_contrast),
data = NULL,
position = position_diverging(0.5),
...,
Expand All @@ -138,7 +138,7 @@ geom_diverging_text <- function(mapping = NULL,

#' @rdname geom_diverging
#' @export
geom_likert_text <- function(mapping = NULL,
geom_likert_text <- function(mapping = ggplot2::aes(!!!auto_contrast),
data = NULL,
position = position_likert(0.5),
...,
Expand All @@ -157,7 +157,7 @@ geom_likert_text <- function(mapping = NULL,

#' @rdname geom_diverging
#' @export
geom_pyramid_text <- function(mapping = NULL,
geom_pyramid_text <- function(mapping = ggplot2::aes(!!!auto_contrast),
data = NULL,
position = position_diverging(0.5),
...,
Expand Down
32 changes: 31 additions & 1 deletion R/hex_bw.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
#' Identify a suitable font color (black or white) given a background HEX color
#'
#' You could use `auto_contrast` as a shortcut of
#' `aes(colour = after_scale(hex_bw(.data$fill)))`. You should use `!!!` to
#' inject it within [ggplot2::aes()] (see examples).
#'
#' @param hex_code Background color in hex-format.
#' @return Either black or white, in hex-format
#' @source Adapted from `saros`
#' @source Adapted from `saros` for `hex_code()` and from
#' <https://github.com/teunbrand/ggplot_tricks?tab=readme-ov-file#text-contrast>
#' for `auto_contrast`.
#' @export
#' @examples
#' hex_bw("#0dadfd")
Expand All @@ -17,6 +23,26 @@
#' stat = "count",
#' size = 2
#' )
#'
#' ggplot(diamonds) +
#' aes(x = cut, fill = color, label = after_stat(count)) +
#' geom_bar() +
#' geom_text(
#' mapping = auto_contrast,
#' position = position_stack(.5),
#' stat = "count",
#' size = 2
#' )
#'
#' ggplot(diamonds) +
#' aes(x = cut, fill = color, label = after_stat(count), !!!auto_contrast) +
#' geom_bar() +
#' geom_text(
#' mapping = auto_contrast,
#' position = position_stack(.5),
#' stat = "count",
#' size = 2
#' )
hex_bw <- function(hex_code) {
rgb_conv <-
lapply(
Expand Down Expand Up @@ -57,3 +83,7 @@ hex_bw_threshold <- function(hex_code, values, threshold) {
x[values < threshold] <- "#000000"
x
}

#' @rdname hex_bw
#' @export
auto_contrast <- ggplot2::aes(colour = after_scale(hex_bw(.data$fill)))
3 changes: 2 additions & 1 deletion R/stat_prop.R
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ geom_prop_bar <- function(mapping = NULL,
#' @rdname geom_prop_bar
#' @importFrom scales percent
#' @export
geom_prop_text <- function(mapping = NULL,
geom_prop_text <- function(mapping = ggplot2::aes(!!!auto_contrast),
data = NULL,
position = ggplot2::position_stack(0.5),
...,
Expand All @@ -275,6 +275,7 @@ geom_prop_text <- function(mapping = NULL,
}

# all defaults to after_stat(prop)
#' @importFrom utils modifyList
StatPropProp <- ggplot2::ggproto(
"StatPropProp",
StatProp,
Expand Down
6 changes: 3 additions & 3 deletions man/geom_diverging.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/geom_prop_bar.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 35 additions & 1 deletion man/hex_bw.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions tests/spelling.R

This file was deleted.

0 comments on commit 2dcf6ca

Please sign in to comment.