public
Last active

Color picker in R

  • Download Gist
color_picker.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("ggplot2", "proxy")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
 
nIncrements <- 36
eachIncrement <- seq(0, 1, len = nIncrements)
colorSweep <- expand.grid(eachIncrement, eachIncrement, eachIncrement)
moduloRemainder <- with(colorSweep, Var1*(nIncrements-1)) %%
floor(sqrt(nIncrements))
quotientFloor <- floor(with(colorSweep, Var1*(nIncrements-1)) /
floor(sqrt(nIncrements)))
 
colorLocations <- with(colorSweep, data.frame(x = moduloRemainder+Var2*8/9,
y = quotientFloor+Var3*8/9))
hexColor <- with(colorSweep, hcl(Var1*360, Var2*150, Var3*100, fixup = FALSE))
colorLocations <- colorLocations[!is.na(hexColor), ]
hexColor <- hexColor[!is.na(hexColor)]
 
par(mai = c(0, 0, 0, 0)) # Plot color picker
plot(colorLocations$x, colorLocations$y, type = "n")
abline(h = -10:60/10, v = -10:60/10, col = gray(2/3))
points(colorLocations$x, colorLocations$y, col = hexColor, pch = 20)
 
# Use your mouse to click on the colors you want in your palette --
# click as many as you want, in the order you want, then:
# if in RStudio: hit [ESC]
# if in Windows RGui: Right click on graph, select [Stop]
whichColor <- locator(type = "o") # Click, then [ESC]/[Stop]!
colorProximities <- proxy::dist(data.frame(whichColor), colorLocations)
whichColor <- apply(colorProximities, 1, which.min)
 
myPalette <- colorRampPalette(hexColor[whichColor], space = "Lab")
 
zp1 <- ggplot(iris, # Plot some iris data
aes(x = Sepal.Length, y = Sepal.Width,
colour = Petal.Width))
zp1 <- zp1 + geom_point(size = 4)
print(zp1) # Default ggplot2 continuous palette
 
zp1 <- zp1 + scale_colour_gradientn(colours = myPalette(99))
print(zp1) # Plotted with your hand-picked palette

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.