Skip to content

Instantly share code, notes, and snippets.

@nacnudus
Last active December 24, 2015 01:19
Show Gist options
  • Save nacnudus/6723265 to your computer and use it in GitHub Desktop.
Save nacnudus/6723265 to your computer and use it in GitHub Desktop.
ggplot axis time format HH:mm:ss
timeHMS_formatter <- function(x) {
x[is.na(x)] <- 0
h <- trunc(x * 24)
m <- trunc(x * 24 * 60)
s <- x * 24 * 60 * 60 %% 60
s <- round(s, 2)
lab <- sprintf('%02d:%02d:%02d', h, m, s) # Format the strings as HH:MM:SS
lab <- gsub('^00:', '', lab) # Remove leading 00: if present
lab <- gsub('^0', '', lab) # Remove leading 0 if present
}
# use it like
yourPlot + scale_y_continuous(label=timeHMS_formatter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment