Skip to content

Instantly share code, notes, and snippets.

@ltrgoddard
Last active October 6, 2018 11:23
Show Gist options
  • Save ltrgoddard/0f8d01bfc937fbcddef7204674b7f23d to your computer and use it in GitHub Desktop.
Save ltrgoddard/0f8d01bfc937fbcddef7204674b7f23d to your computer and use it in GitHub Desktop.
# Takes an ordered vector of numeric values and returns a small bar chart made
# out of Unicode block elements. Works well inside dplyr mutate() or summarise()
# calls on grouped data frames.
sparkbar <- function(values) {
span <- max(values) - min(values)
if(span > 0 & !is.na(span)) {
steps <- round(values / (span / 7))
blocks <- c('▁', '▂', '▃', '▄', '▅', '▆', '▇', '█')
paste(sapply(steps - (min(steps) - 1), function(i) blocks[i]), collapse = '')
} else { NA }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment