gghighlight (version 0.0.1)

gghighlight: Highlight Data With Predicate

Description

gghiglight_line() highlights lines (ggplot2::geom_line()) and gghighlight_points() highlights points (ggplot2::geom_point()) according to the given predicates.

Usage

gghighlight_line(data, mapping, predicate, max_highlight = 5L,
  unhighlighted_colour = ggplot2::alpha("grey", 0.7), use_group_by = TRUE,
  use_direct_label = TRUE, label_key = NULL, ...,
  environment = parent.frame())

gghighlight_point(data, mapping, predicate, max_highlight = 5L, unhighlighted_colour = ggplot2::alpha("grey", 0.7), use_group_by = FALSE, use_direct_label = TRUE, label_key = NULL, ..., environment = parent.frame())

Arguments

data

Default dataset to use for plot. If not already a data.frame, will be converted to one by fortify(). If not specified, must be supplied in each layer added to the plot.

mapping

Default list of aesthetic mappings to use for plot. If not specified, must be supplied in each layer added to the plot.

predicate

Expression to filter data, which is passed to dplyr::filter().

max_highlight

Max number of series to highlight.

unhighlighted_colour

Colour for unhighlited lines/points.

use_group_by

If TRUE, use dplyr::group_by() to evaluate predicate.

use_direct_label

If TRUE, add labels directly on the plot instead of using a legend.

label_key

Column name for label aesthetics.

...

Arguments passed to the corresponding geometry functions (e.g. geom_line()).

environment

If a variable defined in the aesthetic mapping is not found in the data, ggplot will look for it in this environment. It defaults to using the environment in which ggplot() is called.

Details

gghiglight_lines() evaluates predicate by grouped calculation; You must specify the expression that returns one value per group. Aggregate functions (e.g. max(), all()) are usually needed.

gghighlight_points() evaluates predicate by ungrouped calculation; You must specify the expression that returns one value per row.

gghighlight_*() behaves differently, depending on what type of vector the result of the predicate is.

  • If predicate is evaluated into a logical vector, the data series/points filtered by the logical vector will be highlighted.

  • Otherwise, the data series/points are sorted by the result of predicate and the top max_highlight ones will be highlighted.

Examples

Run this code
# NOT RUN {
d <- data.frame(
  idx = c( 1, 1, 1, 2, 2, 2, 3, 3, 3),
  value = c( 1, 2, 3,10,11,12, 9,10,11),
  category = rep(c("a","b","c"), 3),
  stringsAsFactors = FALSE
)

gghighlight_line(d, aes(idx, value, colour = category), max(value) > 10)

# }
# NOT RUN {
# This throws an error because the predicate returns multiple values per group.
gghighlight_line(d, aes(idx, value, colour = category), value > 10)
# }
# NOT RUN {
gghighlight_point(d, aes(idx, value), value > 10, label_key = category)

# }

Run the code above in your browser using DataLab