(gg)plotting colours in CIELAB space?

1,122 views
Skip to first unread message

John McCallum

unread,
Sep 13, 2011, 5:39:23 PM9/13/11
to ggp...@googlegroups.com

Hi

 

{Firstly many thanks Hadley for providing us with such an inspiring graphics tool!}

 

I wish to plot data points from colorimetric measurements made in CIELAB colour space on an a* b* scatterplot with point colour mapped to L*a*b* colour.

I have been able to achieve this using colorspace and lattice libraries but have as yet been unable to do this in ggplot2 with scale_identity

 

What originally inspired me was Hadleys figure 6.11  in “Elegant graphics..”  showing the idea in Luv space.

 

Does anyone have code examples for this kind of thing (or for this figure) or of scale_identity usage they could share?

 

Regards

John McCallum

The contents of this e-mail are confidential and may be subject to legal privilege.
 If you are not the intended recipient you must not use, disseminate, distribute or
 reproduce all or any part of this e-mail or attachments.  If you have received this
 e-mail in error, please notify the sender and delete all material pertaining to this
 e-mail.  Any opinion or views expressed in this e-mail are those of the individual
 sender and may not represent those of The New Zealand Institute for Plant and
 Food Research Limited.

Juan Carlos Borrás

unread,
Sep 14, 2011, 2:39:07 AM9/14/11
to John McCallum, ggp...@googlegroups.com
If the number of categorical variables to plot is known beforehand (i.e. a discrete scale) one could try something 
like this (asuming RGB rather than L*a*b*):

f <- function() {c('#000000', '#FF0000', '#00FF00', '#0000FF', '#FFFF00')}
df=data.frame(x=seq(1,7), y=seq(11,17))
ggplot(df)+geom_bar(aes(x=x, y=y, fill=factor(y)), stat="identity")+scale_fill_manual(values=f())

Parametrizing f to return varying lengths is trivial. Mapping L*a*b* tuples to RGB is left as an exercise to the reader ;-)

A scale_color_Lab() function can't be seen in the docs but such hypothetical function wouldn't differ much from scale_color_hue() IMHO.

This answer is penitence from yesterday's blunder though....
Cheers,
jcb!



--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Hadley Wickham

unread,
Sep 14, 2011, 9:18:32 AM9/14/11
to John McCallum, ggp...@googlegroups.com
Hi John,

It should be pretty trivial if you already have the colours as hex (e.g. hex(LAB(l, a, b))) and you're using scale_colour_identity.  Could you please supply a small reproducible example (https://github.com/hadley/devtools/wiki/Reproducibility) ?

Hadley 

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2



--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Brandon Hurr

unread,
Feb 12, 2013, 6:06:34 AM2/12/13
to LV, ggplot2, john.m...@plantandfood.co.nz
I made this a while ago. It takes La*b* and does some math to get hex values (among others). 

Supply it with a data frame that has columns called L, a, and b and it'll go to town. 

I could probably clean it up and make it a bit faster without loops, but it does what I need it to do. The calculations are based upon the equations at easyRGB.com with one tweak when values were < 0. 

Brandon. 




On Tue, Feb 12, 2013 at 6:05 AM, LV <lva...@gmail.com> wrote:
So here are two attempts to replicate figure 6.11 and neither gives anything close to that image:

myColors <- data.frame("L"=runif(10000, 0,100),"a"=runif(10000, -100, 100),"b"=runif(10000, -100, 100))
myColors <- within(myColors, LAB <- hex(LAB(L, a, b)))
myColors <- within(myColors, Luv <- hex(LUV(L, a, b)))
myColors <- na.omit(myColors)
g <- ggplot(myColors, aes(a, b, color=Luv), size=2)
g + geom_point() + ggtitle ("mycolors")


other <- data.frame("L"=runif(10000),"a"=runif(10000),"b"=runif(10000))
other <- within(other, Luv <- hex(LUV(L, a, b)))
other <- na.omit(other)
g <- ggplot(other, aes(a, b, color=Luv), size=2)
g + geom_point() + ggtitle("other")

The first has some diagonal clustering of the dots, the second is pure random dots. 

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2
 
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Lab2All.R

LV

unread,
Feb 12, 2013, 4:20:55 PM2/12/13
to ggp...@googlegroups.com, LV, john.m...@plantandfood.co.nz
That's a very nice function, and will have a good look at it. In the meantime, another user posted a solution over at SO. I am pasting it here in case somebody comes across this question in the future: 

Reply all
Reply to author
Forward
0 new messages