Skip to content

Instantly share code, notes, and snippets.

@kcha
Forked from johncolby/colorbar.R
Last active November 28, 2015 22:56
Show Gist options
  • Save kcha/8dfdbe1cde0c969dc906 to your computer and use it in GitHub Desktop.
Save kcha/8dfdbe1cde0c969dc906 to your computer and use it in GitHub Desktop.
An R function to generate color bar legends for neuroimaging plots
# Source: http://stackoverflow.com/a/9314880
color.bar <- function(lut, min, max=-min, nticks=11, ticks=seq(min, max, len=nticks), title='') {
scale = (length(lut))/(max-min)
# dev.new(width=1.75, height=5)
plot(c(0,10), c(min,max), type='n', bty='n', xaxt='n', xlab='', yaxt='n', ylab='', main=title)
axis(2, round(ticks, 2), las=1)
for (i in 1:(length(lut))) {
y = (i-1)/scale + min
rect(0,y,10,y+1/scale, col=lut[i], border=NA)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment