scale_colour_gradient2

129 views
Skip to first unread message

Stavros Macrakis

unread,
May 27, 2009, 1:06:31 PM5/27/09
to ggplot2
This looks fine:

ggplot(data=data.frame(a=1:10)) +
   geom_point(aes(x=a,y=a,colour=a)) +
   scale_colour_gradient2(low="red",high="blue",mid="green",midpoint=5)

1 is mapped to red, 5 to blue, and 10 to green, which is good.

However, I am surprised that the scale key does not include the extreme value, namely 1 -- it shows 2,4,6,8,10. (True for scale_colour_gradient as well.)  I suppose it is looking for round numbers, and of course I can override this, and I'm not sure what I'd propose....

However:

ggplot(data=data.frame(a=1:10)) +
   geom_point(aes(x=a,y=a,colour=a)) +
   scale_colour_gradient2(low="red",high="blue",mid="green",midpoint=2)

has a surprising (to me) appearance.  Point (2,2) is indeed green, the stated midpoint, but point (1,1) is not red, but apparently 1/8 red + 7/8 green.  That is, it seems to be creating a balanced scale going from midpoint - max( max(r)-midpoint, midpoint-min(r) ) to midpoint + max( max(r)-midpoint, midpoint-min(r) ). Is that the intended behavior?

           -s


JiHO

unread,
May 27, 2009, 2:15:37 PM5/27/09
to macr...@alum.mit.edu, ggplot2
On 2009-May-27 , at 13:06 , Stavros Macrakis wrote:

> This looks fine:
>
> ggplot(data=data.frame(a=1:10)) +
> geom_point(aes(x=a,y=a,colour=a)) +
>
> scale_colour_gradient2(low="red",high="blue",mid="green",midpoint=5)
>
> 1 is mapped to red, 5 to blue, and 10 to green, which is good.
>
> However, I am surprised that the scale key does not include the
> extreme value, namely 1 -- it shows 2,4,6,8,10. (True for
> scale_colour_gradient as well.) I suppose it is looking for round
> numbers, and of course I can override this, and I'm not sure what
> I'd propose....

I have also been bitten by that before: it is sometimes annoying that
the legend does not span the whole range of the data. However finding
the correct extremes is a tricky problem which might not have a
general solution (you would not want your legend to start at 0.587241
if that was your minimum). So the only sensible solution I can see
would be to force the min and max of the legend to match the min and
max of the data if those are integers (or close to integers). But that
would require writing something on top (or re-writing) pretty() which
is currently used to select the legends.

Plus, in the gradient2 case you have one additional constraint: you
probably also want to see your middle value. Therefore, in your case,
you would want:

1 .. 2 .... 10

Unless you use two different intervals (i.e. call pretty 2 times) for
the part below 2 and above 2, I don't see an easy solution.

> However:
>
> ggplot(data=data.frame(a=1:10)) +
> geom_point(aes(x=a,y=a,colour=a)) +
>
> scale_colour_gradient2(low="red",high="blue",mid="green",midpoint=2)
>
> has a surprising (to me) appearance. Point (2,2) is indeed green,
> the stated midpoint, but point (1,1) is not red, but apparently 1/8
> red + 7/8 green. That is, it seems to be creating a balanced scale
> going from midpoint - max( max(r)-midpoint, midpoint-min(r) ) to
> midpoint + max( max(r)-midpoint, midpoint-min(r) ). Is that the
> intended behavior?

I have no clue definitive answer once again. But I would guess it is
indeed intended because with a symmetric scale, the saturation of the
color is an indication of how far you are from the middle of the
scale. This information would be lost otherwise and could be
perceptually misleading (i.e. 1 and 10 would appear on-par in the plot).

JiHO
---
http://jo.irisson.free.fr/


hadley wickham

unread,
May 28, 2009, 5:21:38 PM5/28/09
to macr...@alum.mit.edu, ggplot2
On Thu, May 28, 2009 at 5:06 AM, Stavros Macrakis <macr...@gmail.com> wrote:
> This looks fine:
>
> ggplot(data=data.frame(a=1:10)) +
>    geom_point(aes(x=a,y=a,colour=a)) +
>    scale_colour_gradient2(low="red",high="blue",mid="green",midpoint=5)
>
> 1 is mapped to red, 5 to blue, and 10 to green, which is good.
>
> However, I am surprised that the scale key does not include the extreme
> value, namely 1 -- it shows 2,4,6,8,10. (True for scale_colour_gradient as
> well.)  I suppose it is looking for round numbers, and of course I can
> override this, and I'm not sure what I'd propose....

As Jiho said, getting the tick marks right is really difficult. I
hope to have a student work on this in the future - and perhaps make
it easier to select the algorithm to use.

>
> However:
>
> ggplot(data=data.frame(a=1:10)) +
>    geom_point(aes(x=a,y=a,colour=a)) +
>    scale_colour_gradient2(low="red",high="blue",mid="green",midpoint=2)
>
> has a surprising (to me) appearance.  Point (2,2) is indeed green, the
> stated midpoint, but point (1,1) is not red, but apparently 1/8 red + 7/8
> green.  That is, it seems to be creating a balanced scale going from
> midpoint - max( max(r)-midpoint, midpoint-min(r) ) to midpoint + max(
> max(r)-midpoint, midpoint-min(r) ). Is that the intended behavior?

Yes, that's right.

Hadley


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

Reply all
Reply to author
Forward
0 new messages