{emphatic} v0.1.3 - highlight data.frame output with ANSI - Added legends & support for vectors

emphatic

Lifecycle: experimental R build status

{emphatic} augments the output of data.frames, matrices and vectors in R by adding user-defined ANSI highlighting.

{emphatic} v0.1.3 Update

This update adds:

  • support for simple atomic vectors
  • option to add legends when colour is applied using ggplot2 colour scales

Support for Vectors (1)

A simple random vector

v <- sample(letters[1:5], 50, TRUE, (1:5)^2)
v
 [1] "d" "e" "e" "e" "c" "e" "d" "c" "b" "e" "c" "e" "e" "b" "e" "d" "b" "d" "b"
[20] "c" "d" "a" "d" "d" "d" "c" "d" "c" "d" "d" "d" "e" "c" "d" "d" "b" "d" "d"
[39] "e" "e" "b" "e" "d" "e" "c" "e" "c" "c" "d" "d"

Highlight vector where a value is the third-identical-value-in-a-row

v %>%
  hl_vec('red', .x == lag(.x) & .x == lag(lag(.x)))
[ 1] "d" "e" "e" "e" "c" "e" "d" "c" "b" "e" "c" "e" "e" "b" "e" "d" "b" "d" "b"
[20] "c" "d" "a" "d" "d" "d" "c" "d" "c" "d" "d" "d" "e" "c" "d" "d" "b" "d" "d"
[39] "e" "e" "b" "e" "d" "e" "c" "e" "c" "c" "d" "d"

Support for Vectors (2)

A simple random vector

v <- sample(5, 50, TRUE, (1:5)^2)
v
 [1] 4 5 5 5 3 5 4 3 2 5 3 5 5 2 5 4 2 4 2 3 4 1 4 4 4 3 4 3 4 4 4 5 3 4 4 2 4 4
[39] 5 5 2 5 4 5 3 5 3 3 4 4

Highlight vector with a colour scale

v %>%
  hl_vec(scale_colour_viridis_c())
[ 1] 4 5 5 5 3 5 4 3 2 5 3 5 5 2 5 4 2 4 2 3 4 1 4 4 4 3 4 3 4 4 4 5 3 4 4 2 4 4
[39] 5 5 2 5 4 5 3 5 3 3 4 4

Highlight vector with a colour scale. Include legend

v %>%
  hl_vec(scale_colour_viridis_c(), show_legend = TRUE)
[ 1] 4 5 5 5 3 5 4 3 2 5 3 5 5 2 5 4 2 4 2 3 4 1 4 4 4 3 4 3 4 4 4 5 3 4 4 2 4 4
[39] 5 5 2 5 4 5 3 5 3 3 4 4

legend:  1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0

Legends

Legends can be added whenever a ggplot2 colour scale is used to highlight an object.

mtcars %>%
  head() %>%
  hl(scale_colour_viridis_c(), cols = mpg, dest_cols = .all, show_legend = TRUE)
                     mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4           21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag       21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710          22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive      21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout   18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant             18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

mpg:  18.5 19.0 19.5 20.0 20.5 21.0 21.5 22.0 22.5

Multiple Legends

Multiple legends are possible, but there’s definitely diminishing returns in interpretability.

mtcars %>%
  head() %>%
  mutate(cyl = as.factor(cyl)) %>%
  hl(scale_colour_viridis_c(), cols = mpg, show_legend = TRUE) %>%
  hl(scale_colour_discrete(), cols = cyl, show_legend = TRUE)
     mpg cyl disp  hp drat    wt  qsec vs am gear carb
1   21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
2   21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
3   22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
4   21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
5   18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
6   18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

mpg:  18.5 19.0 19.5 20.0 20.5 21.0 21.5 22.0 22.5
cyl:  4 6 8

Installation

You can install from GitHub with:

# install.package('remotes')
remotes::install_github('coolbutuseless/emphatic')

See the online documentation for vignettes and more examples.

Slideshow