library(ggplot2)
library(forcats)
library(dplyr)
library(pichor)

Minimal example

keys_chords %>% 
  highlight_key_sequence(key_sequence = list(c(1, 2, 3), c(2, 3, 4))) %>% 
  ggpiano() + 
  facet_wrap(~ seq_name)
#> NULL

Slightly larger example

A version of “Let it be” by Beatles initially has chords G, D, Em, C, G, D, C.

key_seq <- lapply(chords, get_keys)
keys_chords %>% 
  highlight_key_sequence(key_sequence = key_seq) %>% 
  ggpiano() + 
  facet_wrap(~ seq_name)
#> NULL

And get more helpful facet panel title by providing sequence_names argument to highlight_key_sequence:

And for exmple include seq_no in a new column seq_lbl (that are then converted to a factor with the right ordering):

keys_chords %>% 
  highlight_key_sequence(key_sequence = key_seq,
                         sequence_names = chord_names) %>% 
  mutate(seq_lbl = paste0(seq_name, " (#", seq_no, ")")) %>% 
  mutate(seq_lbl = forcats::fct_inorder(seq_lbl)) %>% 
  ggpiano() + 
  facet_wrap(~ seq_lbl)
#> NULL

Finding better inversions

Currently only one method is available for finding better inversions. It is minimising the distances between keys in consecutive chords, where distance is the number of keys not used anymore plus new keys.
And with an exhaustive approach that is only feasible for shorter sequences.

opt_res <- optim_min_dist_exhaustive(key_sequence = key_seq)
keys_chords %>% 
  highlight_key_sequence(key_sequence = opt_res$best,
                         sequence_names = chord_names) %>% 
  ggpiano() + 
  facet_wrap(~ seq_name) +
  labs(title = "Best inversions")
#> NULL

keys_chords %>% 
  highlight_key_sequence(key_sequence = opt_res$worst,
                         sequence_names = chord_names) %>% 
  ggpiano() + 
  facet_wrap(~ seq_name) +
  labs(title = "Worst inversions")
#> NULL