knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-"
)

# if ragg is installed, use it.
if (requireNamespace("ragg", quietly = TRUE)) {
  knitr::opts_chunk$set(
    dev = "ragg_png"
  )
}

gghighlight

R build status CRAN_Status_Badge

Highlight geoms in ggplot2.

Installation

install.packages("gghighlight")

# Or the development version from GitHub:
# install.packages("devtools")
devtools::install_github("yutannihilation/gghighlight")

Example

(For the full version, please refer to Introduction to gghighlight).

Suppose we have a data that has so many series that it is hard to identify them by their colours as the differences are so subtle.

set.seed(2)
d <- purrr::map_dfr(
  letters,
  ~ data.frame(
      idx = 1:400,
      value = cumsum(runif(400, -1, 1)),
      type = .,
      flag = sample(c(TRUE, FALSE), size = 400, replace = TRUE),
      stringsAsFactors = FALSE
    )
)
library(ggplot2)

ggplot(d) +
  geom_line(aes(idx, value, colour = type))

With gghighlight(), we can highlight the lines whose max values are larger than 20:

library(gghighlight)

p <- ggplot(d) +
  geom_line(aes(idx, value, colour = type)) +
  gghighlight(max(value) > 20)

p

The result is a usual ggplot object, so it is fully customizable. For example, it can be used with custom themes and facets.

p + theme_minimal()

p + theme_minimal() + facet_wrap(~ type)

gghighlight() can highlight almost any geoms. For more details, please read Introduction to gghighlight.



yutannihilation/gghighlight documentation built on Jan. 28, 2024, 11:01 a.m.