I spotted this blog post about interpolating colors in LAB with d3.js. Since I think the scales
package in R
doesn’t get the attention it deserves, I wanted to replicate the example in R
.
# do d3 interpolateLab in R with scales
# https://translate.google.com/translate?hl=en&sl=ja&tl=en&u=http%3A%2F%2Fblog.aagata.ciao.jp%2F%3Feid%3D116
library(scales)
plot(
0:10,
col=col_numeric(
palette=c("white","steelblue"),
domain=c(0,10)
)(0:10),
pch=16,cex=2
)
plot(
0:10,
col=col_numeric(
palette=c("blue","red"),
domain=c(0,10)
)(0:10),
pch=16,cex=2
)
To extend the theme of parallel R and JavaScript examples (see yesterday’s nesting and summarizing example), I’ll add a bonus example using V8
to get the colors in R
but using d3.js
.
# for fun get colors from d3 using V8
# using same code as http://blog.aagata.ciao.jp/?eid=116
library(V8)
ctx <- v8()
ctx$source("https://d3js.org/d3.v3.min.js")
ctx$eval(
'
//http://blog.aagata.ciao.jp/?eid=116
var cscale = d3.scale
.linear()
.domain([0, 10])
.range(["blue", "red"])
.interpolate(d3.interpolateLab);
var colors = d3.range(0,10).map(function(d){return cscale(d)});
'
)
colors <- ctx$get("colors")
plot(1:10, col=colors, pch=16, cex=2)