Binned colour gradients: scale_steps
Mix.install([
{:giggly, path: Path.join(__DIR__, "..")},
{:kino, "~> 0.19"}
])
alias Giggly.Plot
render = fn plot ->
plot
|> Giggly.to_svg()
|> IO.iodata_to_binary()
|> Kino.Image.new(:svg)
end
About this notebook
This notebook follows the examples on ggplot2's reference page for
scale_colour_steps(), which covers
scale_colour_steps2(), scale_colour_stepsn(), scale_fill_steps(), scale_fill_steps2()
and scale_fill_stepsn() too, in the order the page gives them. Each example shows ggplot2's code
first and then Giggly's. An example Giggly cannot draw yet keeps its ggplot2 code and names what
it is waiting for, so the page is also a list of what remains.
The steps scales are the binned counterparts of the gradient scales: they cut a continuous column
into bins and give each bin one colour from a gradient. scale_colour_steps() runs from a low
colour to a high one, scale_colour_steps2() diverges from a low colour through a middle one to a
high one, around a midpoint, and scale_colour_stepsn() takes its colours from as many as it is
given; the fill versions do the same for fill. Giggly has all six,
Giggly.Plot.scale_color_steps/2, Giggly.Plot.scale_color_steps2/2,
Giggly.Plot.scale_color_stepsn/2, Giggly.Plot.scale_fill_steps/2,
Giggly.Plot.scale_fill_steps2/2 and Giggly.Plot.scale_fill_stepsn/2, with ggplot2's options,
and reads them back with colour steps, as guide_coloursteps() draws at its defaults. A plot is
described with Giggly.Plot and drawn with Giggly.to_svg/2, which returns an SVG document. The
setup cell aliases Giggly.Plot, so the cells write Plot.new/2, and defines render, which draws
a plot and shows it below the cell with Kino.Image.
A binned gradient
The examples draw from a table of random numbers R makes after set.seed(1).
set.seed(1)
df <- data.frame(
x = runif(100),
y = runif(100),
z1 = rnorm(100)
)
# Use scale_colour_steps for a standard binned gradient
ggplot(df, aes(x, y)) +
geom_point(aes(colour = z1)) +
scale_colour_steps()
The cell writes out df's x, y and z1 as R made them, each number to the last digit R
holds. Each point takes the colour of its bin, from #132B43 at the lowest to #56B1F7 at the
highest, each bin's colour the gradient's at the bin's middle.
df = %{
"x" => [
0.2655086631421, 0.37212389963679016, 0.5728533633518964, 0.9082077899947762,
0.2016819310374558, 0.8983896849676967, 0.9446752686053514, 0.6607977924868464,
0.6291140438988805, 0.061786270467564464, 0.20597457489930093, 0.17655675252899528,
0.6870228466577828, 0.384103718213737, 0.7698414199985564, 0.49769924208521843,
0.7176185082644224, 0.9919060948304832, 0.3800351794343442, 0.7774452213197947,
0.9347052311059088, 0.21214252128265798, 0.651673766085878, 0.12555509596131742,
0.26722066872753203, 0.3861140925437212, 0.013390333158895373, 0.3823879570700228,
0.86969084572047, 0.3403489966876805, 0.48208011547103524, 0.5995658254250884,
0.4935413070488721, 0.1862176014110446, 0.8273733186069876, 0.6684667381923646,
0.7942398607265204, 0.10794362588785589, 0.723710946040228, 0.4112744296435267,
0.8209462941158563, 0.6470601938199252, 0.7829327622894198, 0.5530363116413355,
0.5297195801977068, 0.7893562316894531, 0.023331202333793044, 0.47723006503656507,
0.7323137386702001, 0.6927315564826131, 0.4776196221355349, 0.8612094768323004,
0.4380971072241664, 0.24479727703146636, 0.07067904714494944, 0.09946616017259657,
0.3162717071827501, 0.5186342631932348, 0.6620050764176995, 0.4068301871884614,
0.9128759242594242, 0.29360337276011705, 0.4590657262597233, 0.3323946741875261,
0.6508704670704901, 0.2580167807172984, 0.4785452482756227, 0.7663106706459075,
0.08424691436812282, 0.875321330036968, 0.33907293784432113, 0.8394403501879424,
0.3466834891587496, 0.3337749307975173, 0.4763512450736016, 0.8921983358450234,
0.8643394706305116, 0.389989543473348, 0.7773206988349557, 0.9606179972179234,
0.4346594847738743, 0.7125146787147969, 0.399994368897751, 0.3253521518781781,
0.7570871480274945, 0.20269225514493883, 0.7111212224699557, 0.12169192102737725,
0.24548851395957172, 0.14330437942408025, 0.2396294150967151, 0.05893437727354467,
0.6422882585320622, 0.8762692126911134, 0.7789146774448454, 0.7973088258877397,
0.45527445361949503, 0.4100840820465237, 0.8108702427707613, 0.6049332902766764
],
"y" => [
0.6547239280771464, 0.35319727193564177, 0.2702601458877325, 0.9926840611733496,
0.6334932644385844, 0.2132081352174282, 0.12937234807759523, 0.47811803431250155,
0.9240744698327035, 0.59876096714288, 0.9761706949211657, 0.7317925118841231,
0.35672691208310425, 0.43147369055077434, 0.148211560677737, 0.013077575480565429,
0.7155660660937428, 0.10318423574790359, 0.4462843486107886, 0.6401010451372713,
0.991838620044291, 0.49559357785619795, 0.4843495243694633, 0.17344233486801386,
0.7548209445085377, 0.4538954892195761, 0.511169783771038, 0.20754511328414083,
0.22865814273245633, 0.5957119963131845, 0.5748721982818097, 0.07706438028253615,
0.0355405795853585, 0.6427954921964556, 0.9286151996348053, 0.5980924223549664,
0.5609007480088621, 0.5260277239140123, 0.985095223877579, 0.5076418223325163,
0.6827880786731839, 0.6015412176493555, 0.23886867775581777, 0.25816592667251825,
0.7293096233624965, 0.45257083140313625, 0.17512676841579378, 0.7466982696205378,
0.10498764039948583, 0.864544949028641, 0.6146449716761708, 0.557159538846463,
0.32877731905318797, 0.45313144545070827, 0.5004409726243466, 0.18086636113002896,
0.5296306028030813, 0.07527574570849538, 0.2777559326495975, 0.2126995192375034,
0.28479048097506166, 0.8950941029470414, 0.44623532332479954, 0.7799848897848278,
0.8806190348695964, 0.4131242095027119, 0.06380848051048815, 0.33548749168403447,
0.7237259466201067, 0.33761533349752426, 0.6304141224827617, 0.8406145540066063,
0.8561316647101194, 0.3913592810276896, 0.3804938856046647, 0.8954454259946942,
0.6443157629109919, 0.7410786487162113, 0.6053034465294331, 0.9030816114973277,
0.2937301550991833, 0.1912601098883897, 0.8864509433042258, 0.5033394857309759,
0.8770575430244207, 0.18919362244196236, 0.758103052387014, 0.7244988926686347,
0.9437248182948679, 0.547646587016061, 0.7117438677232713, 0.3889050998259336,
0.10087312618270516, 0.9273020885884762, 0.28323250031098723, 0.5905731588136405,
0.11036060494370759, 0.840507032116875, 0.3179636844433844, 0.7828513369895518
],
"z1" => [
-0.6203666772241242, 0.04211587314423524, -0.9109216485524455, 0.15802877240407498,
-0.6545846439188178, 1.767287269372646, 0.7167074760172057, 0.9101742294952272,
0.3841853578263446, 1.6821760805194184, -0.635736453948977, -0.461644730360566,
1.432282238541663, -0.6506963533103667, -0.20738074360196543, -0.3928079294419839,
-0.31999286854850684, -0.27911330297655895, 0.494188331267827, -0.17733048226960638,
-0.5059574621142574, 1.3430388251704115, -0.21457940854686872, -0.17955653004338712,
-0.10019074121356196, 0.7126663070514053, -0.07356440412632634, -0.037634171467047915,
-0.6816604787556569, -0.32427027224631905, 0.06016044043451516, -0.5888944862596638,
0.5314961926325724, -1.5183940817867871, 0.3065578607897656, -1.536449823537586,
-0.3009761268366107, -0.5282799044450062, -0.6520947806809988, -0.05689677784739255,
-1.9143594256800118, 1.1765833120185625, -1.6649724362120033, -0.4635304014723856,
-1.1159201050428453, -0.7508190011934479, 2.087166545628347, 0.017395619693251662,
-1.2863005304343256, -1.6406055344185784, 0.45018710127265554, -0.018559832714637983,
-0.3180683745438444, -0.9293621474537024, -1.4874603101414847, -1.0751922966156808,
1.0000288037139142, -0.6212666947968234, -1.3844268473844914, 1.8692906224235806,
0.4251003773724479, -0.23864710091303276, 1.05848304870902, 0.8864226513749359,
-0.6192430482311471, 2.2061024645404674, -0.25502703014101524, -1.4244946502128084,
-0.14439960195421941, 0.20753833923234477, 2.3079783990593614, 0.10580236789371146,
0.45699880542341353, -0.07715293535653098, -0.3340008423665445, -0.03472602831127619,
0.7876396056301618, 2.075245008652285, 1.027392438763768, 1.2079083983867038,
-1.231323421558044, 0.9838955700533791, 0.21992480366065129, -1.4672500290922426,
0.5210227426481387, -0.158754604716016, 1.4645873119697974, -0.7660819996046648,
-0.43021175392854655, -0.9261094973774372, -0.17710396143654025, 0.4020117794863379,
-0.7317481731196064, 0.8303731679816738, -1.208082786304465, -1.0479844128077416,
1.441157706844281, -1.015847465304649, 0.41197471231751487, -0.3810760511089196
]
}
df
|> Plot.new(%{x: :x, y: :y})
|> Plot.geom_point(%{colour: :z1})
|> Plot.scale_color_steps()
|> render.()
A diverging binned gradient
# Get a divergent binned scale with the *2 variant
ggplot(df, aes(x, y)) +
geom_point(aes(colour = z1)) +
scale_colour_steps2()
df
|> Plot.new(%{x: :x, y: :y})
|> Plot.geom_point(%{colour: :z1})
|> Plot.scale_color_steps2()
|> render.()
Colours of your own
# Define your own colour ramp to extract binned colours from
ggplot(df, aes(x, y)) +
geom_point(aes(colour = z1)) +
scale_colour_stepsn(colours = terrain.colors(10))
R's terrain.colors(10) is written out as the ten colours it returns.
terrain =
~w(#00A600 #2DB600 #63C600 #A0D600 #E6E600 #E8C32E #EBB25E #EDB48E #F0C9C0 #F2F2F2)
df
|> Plot.new(%{x: :x, y: :y})
|> Plot.geom_point(%{colour: :z1})
|> Plot.scale_color_stepsn(colors: terrain)
|> render.()