Mapping variables to lightness

49 views
Skip to first unread message

Karl Ove Hufthammer

unread,
Oct 7, 2009, 10:42:49 AM10/7/09
to ggplot2
Is it possible to map variables to lightness/brightness/luminance with
ggplot2?

Here's a simple example of a dataset this could be useful on:

n=16
x=rnorm(n)
y=rnorm(n)
l=c(3,5,n-8)
time=unlist(lapply(l,seq_len))
z=rep(letters[1:3],l)
d=data.frame(x,y,z,time)
qplot(x,y,colour=z,group=z,size=I(3),geom="path")

This draws three paths of different lengths and colours. If 'x' and y
'is' position and 'time' is time (either absolute or relative), it
would useful to use the lightness to indicate time, where the old
positions fade away. (An alternative is to use 'alpha', but for
various reasons I prefer not too.)

So, is this possible?

There is in fact two useful ways the lightness could be mapped. One
uses an absolute scale, so that time = 1 is lightest and time = 8 is
darkest (which should be the colour the above code now gives us, not
black). Then all of short pink line will be very light. This is the
way 'size=time' works.

The other one is based on a relative scale, e.g., a scale for each
group. Then the start of the pink line will be very light (e.g.
white), and the end will have full colour.

Both alternatives will be useful in different circumstances.

--
Karl Ove Hufthammer

baptiste auguie

unread,
Oct 7, 2009, 10:57:11 AM10/7/09
to Karl Ove Hufthammer, ggplot2
Hi,

(maybe I missed an obvious built-in solution)

I think you could probably tweak scale_colour_hue() a little to adjust
the luminosity rather than the hue. The tricky part is probably to
define a good mapping. Below is (what I think is) the relevant bit
from scale_colour_discrete.r,


ScaleHue <- proto(ScaleColour, expr={

[... stuff omitted ...]

grDevices::hcl(
h = rotate(seq(.$h[1], .$h[2], length = n)),
c =.$c,
l =.$l
)

[...]


HTH,

baptiste

baptiste auguie

unread,
Oct 7, 2009, 11:01:37 AM10/7/09
to Karl Ove Hufthammer, ggplot2
Oops, where I said mapping might prove "tricky", "trivial" was
probably more appropriate (hue has a fancy mapping on a circle, whilst
luminance is probably simply linear from 0 to 100).

baptiste

baptiste auguie

unread,
Oct 7, 2009, 11:36:23 AM10/7/09
to Karl Ove Hufthammer, ggplot2
I just uploaded an experimental version here,

source("http://ggextra.googlecode.com/svn/trunk/inst/tests/colour-luminance.r")

dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
d <- qplot(carat, price, data=dsamp, colour=clarity)

d + scale_colour_luminance()


HTH,

baptiste

Karl Ove Hufthammer

unread,
Oct 8, 2009, 3:31:23 AM10/8/09
to ggp...@googlegroups.com
Quoting baptiste auguie <bapt...@googlemail.com>:

> I just uploaded an experimental version here,
>
> source("http://ggextra.googlecode.com/svn/trunk/inst/tests/colour-luminance.r")
>
> dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
> d <- qplot(carat, price, data=dsamp, colour=clarity)
>
> d + scale_colour_luminance()

Thanks. It works fine for setting the luminance based on the 'colour'
aesthetic, but that's not what I need; I wish to set the hue based on
the 'colour' aesthetic and set the luminance based on a different
('luminance') aesthetic, so that one line (in my original example) is
pink with various luminance values, one is green with various
luminance values, and one is blue with various luminance values.

In other words, the hue and the luminance should be set independently,
just like colour and size and alpha can be.

--
Karl Ove Hufthammer

baptiste auguie

unread,
Oct 8, 2009, 4:06:30 AM10/8/09
to ggplot2
On Thu, Oct 8, 2009 at 9:31 AM, Karl Ove Hufthammer
<Karl.Hu...@math.uib.no> wrote:

> In other words, the hue and the luminance should be set independently,
> just like colour and size and alpha can be.
>
> --
> Karl Ove Hufthammer
>

As far as I understand you'd need to modify most if not all existing
geoms to add this new luminance aesthetic, and you would also have to
deal with the interaction between colour and luminance. (presumably by
converting the colour and fill to hcl in all geoms). Let's hope I
missed something more obvious.


baptiste

hadley wickham

unread,
Oct 9, 2009, 3:17:01 PM10/9/09
to baptiste auguie, ggplot2
> As far as I understand you'd need to modify most if not all existing
> geoms to add this new luminance aesthetic, and you would also have to
> deal with the interaction between colour and luminance. (presumably by
> converting the colour and fill to hcl in all geoms). Let's hope I
> missed something more obvious.

ggplot2 definitely has the capability for scales to have multiple
input variables for a single output. I don't think there are any
scales that currently do this, but I did use to have a scale where you
could map variables to hue, chroma and luminance separately. I ended
up taking it out because it was very difficult to create useful plots
with it.

Hadley

--
http://had.co.nz/

baptiste auguie

unread,
Oct 10, 2009, 11:54:38 AM10/10/09
to hadley wickham, ggplot2
On Fri, Oct 9, 2009 at 9:17 PM, hadley wickham <h.wi...@gmail.com> wrote:
>
> ggplot2 definitely has the capability for scales to have multiple
> input variables for a single output.  I don't think there are any
> scales that currently do this, but I did use to have a scale where you
> could map variables to hue, chroma and luminance separately.

That makes sense, there is no need to modify other scales as I wrongly
hypothesized.

As far as I understand though, such a scale would require adding a
"luminance" parameter to all relevant geoms, right?

baptiste

hadley wickham

unread,
Oct 11, 2009, 5:10:39 PM10/11/09
to baptiste auguie, ggplot2
> That makes sense, there is no need to modify other scales as I wrongly
> hypothesized.
>
> As far as I understand though, such a scale would require adding a
> "luminance" parameter to all relevant geoms, right?

No, because the scale would emit the colour aesthetic. It's a
different situation to alpha, because colour and alpha are
independent, while luminance and hue both contribute to colour.

Hadley

--
http://had.co.nz/

Ian Carroll

unread,
Oct 25, 2016, 10:24:08 AM10/25/16
to ggplot2, bapt...@googlemail.com

scales that currently do this, but I did use to have a scale where you
could map variables to hue, chroma and luminance separately.  I ended
up taking it out because it was very difficult to create useful plots
with it.



Sorry to revive such an old thread, but I'm looking for this functionality right now. It seems quite natural to want hue and luminance mapped to different variables. Did the feature ever get re-implemented somehow?

Reply all
Reply to author
Forward
0 new messages