test_df <- head(mtcars, 10)

Highlighting a numeric vector using expressions

Highlight locations in a numeric vector which match an expression.

sample(10, 30, replace = TRUE, prob = 1:10) %>%
  hl_vec('green', .x < 3) %>%
  hl_vec('blue', .x > 7)
[ 1]  9  8  7  3  9  3  3  6  6 10  9 10  6  8  5  7  6  1  8  5  3  9  6 10  9
[26]  8 10  8  4  9

Highlighting a numeric vector with a colour scale

Colour a numeric vector using a ggplot colour scale.

sample(10, 30, replace = TRUE, prob = 1:10) %>%
  sort() %>%
  hl_vec(scale_colour_viridis_c(option = 'A'))
[ 1]  1  3  3  3  3  4  5  5  6  6  6  6  6  7  7  8  8  8  8  8  9  9  9  9  9
[26]  9 10 10 10 10

Highlighting a character vector

Highlight elements of a vector when they are identical to the previous element.

sample(letters[1:5], 30, replace = TRUE, prob = 1:5) %>%
  hl_vec('red', .x == lag(.x))
[ 1] "e" "d" "d" "b" "e" "b" "a" "c" "c" "e" "e" "e" "c" "d" "c" "d" "c" "a" "d"
[20] "c" "a" "e" "c" "e" "e" "d" "e" "d" "b" "d"

Adding a legend when using a colour scale

sample(letters[1:5], 30, replace = TRUE, prob = 1:5) %>%
  hl_vec(scale_colour_viridis_d(), show_legend = TRUE)
[ 1] "e" "d" "d" "b" "e" "b" "a" "c" "c" "e" "e" "e" "c" "d" "c" "d" "c" "a" "d"
[20] "c" "a" "e" "c" "e" "e" "d" "e" "d" "b" "d"

legend:  a b c d e

Highlighting by location index

sample(letters[1:5], 30, replace = TRUE, prob = 1:5) %>%
  hl_vec('blue', seq(1, 30, 2))
[ 1] "e" "d" "d" "b" "e" "b" "a" "c" "c" "e" "e" "e" "c" "d" "c" "d" "c" "a" "d"
[20] "c" "a" "e" "c" "e" "e" "d" "e" "d" "b" "d"

Highlighting with alternating values

sample(letters[1:5], 30, replace = TRUE, prob = 1:5) %>%
  hl_vec(c('blue', 'green'))
[ 1] "e" "d" "d" "b" "e" "b" "a" "c" "c" "e" "e" "e" "c" "d" "c" "d" "c" "a" "d"
[20] "c" "a" "e" "c" "e" "e" "d" "e" "d" "b" "d"