scale_fill_gradient2: Color Range?

8,197 views
Skip to first unread message

CK

unread,
Oct 28, 2011, 6:29:21 PM10/28/11
to ggplot2
I'm trying to produce a heatmap of values with the following color
gradient ranges: -

Low, -1.3 to -5, increasing green peaking at -5
Mid, >-1.3 to <1.3, White
High, 1.3 to 5, increasing red peaking at 5

The closest I can get is: -

p <- ggplot(z, aes(variable, Gene_Symbol)) +
geom_tile(aes(fill=value)) +
scale_fill_gradient2(limits=c(-5, 5), midpoint=0, low =
muted("green"), mid="white", high = muted("red"))


This is quite close, however all values <0 are red and all values >0
are green. Is there a way to specify a midpoint range (i.e. >-1.3 and
<1.3) rather than a single value? If not, is there any other way to
get this result?

Thanks

Brandon Hurr

unread,
Oct 29, 2011, 10:00:27 AM10/29/11
to CK, ggplot2
I think you might be able to get what you want with scale_gradientn(). You can specify more breaks that way and keep the middle of your data more white. 


--
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

CK

unread,
Oct 31, 2011, 9:45:59 PM10/31/11
to ggplot2
Hi thanks for your reply. I've taken a look at scale_gradientn() and
it looks promising but I'm not quite getting the results I would like
yet. Here's what I have: -

breaks = c(-5, -1.3, 0, 1.3, 5)
mycolours = c("green", "grey50", "white", "grey50", "red")

p <- ggplot(z, aes(variable, Gene_Symbol)) +
geom_tile(aes(fill=value)) +
scale_fill_gradientn(colour = mycolours, breaks=breaks,
labels=format(breaks))
p


The result is a heatmap that appears mostly green, not quite what was
intended.

Thanks for any suggestions.

Brandon Hurr

unread,
Nov 1, 2011, 4:48:38 AM11/1/11
to CK, ggplot2
Have you got some example data we could play with... really hard to go on "it's a little too green." 

Brandon

CK

unread,
Nov 1, 2011, 12:17:12 PM11/1/11
to ggplot2
Sure, tsv file below.

## BOF ##

"Gene_Symbol" "variable" "value"
"1" "Angptl4" "a" 1
"2" "Acsbg1" "a" 1
"3" "Acsl3" "a" 1
"4" "Vnn1" "a" 1
"5" "Bnip3" "a" 1
"6" "1300010F03Rik" "a" 1
"7" "Cpt1a" "a" 1
"8" "Scd1" "a" 1
"9" "Fgfbp1" "a" 1
"10" "Lpcat3" "a" 1
"11" "AU018091" "a" 1
"12" "Slc25a20" "a" 1
"13" "Angptl4" "b" 17.6813585102252
"14" "Acsbg1" "b" 3.23071906944177
"15" "Acsl3" "b" 2.09114873595659
"16" "Vnn1" "b" 1.47224148183269
"17" "Bnip3" "b" 1.56854362291394
"18" "1300010F03Rik" "b" 1.51226627145813
"19" "Cpt1a" "b" 2.45857362657405
"20" "Scd1" "b" 1.50149557861824
"21" "Fgfbp1" "b" 1.53984001699989
"22" "Lpcat3" "b" 1.63901833877644
"23" "AU018091" "b" 3.20941747895525
"24" "Slc25a20" "b" 1.72643866703735
"25" "Angptl4" "c" 3.50881064670223
"26" "Acsbg1" "c" 1.60354399360629
"27" "Acsl3" "c" 1.47455592016899
"28" "Vnn1" "c" 1.32082251130455
"29" "Bnip3" "c" 1.62811856958207
"30" "1300010F03Rik" "c" 1.39708692575549
"31" "Cpt1a" "c" 1.5092564116014
"32" "Scd1" "c" 1.31564197567378
"33" "Fgfbp1" "c" 1.58600261563202
"34" "Lpcat3" "c" 1.36521678761718
"35" "AU018091" "c" 1.46924525254286
"36" "Slc25a20" "c" 1.41594780064409
"37" "Angptl4" "d" 3.67185999917645
"38" "Acsbg1" "d" 1.41953210273859
"39" "Acsl3" "d" 1.41537898173767
"40" "Vnn1" "d" 1.32873369521246
"41" "Bnip3" "d" 1.65454269790977
"42" "1300010F03Rik" "d" 1.35625807855527
"43" "Cpt1a" "d" 1.44201334571711
"44" "Scd1" "d" 1.34972111898344
"45" "Fgfbp1" "d" 1.32068746493222
"46" "Lpcat3" "d" 1.36647137016739
"47" "AU018091" "d" 1.49519247145254
"48" "Slc25a20" "d" 1.31564843552276


## EOF ##

Brandon Hurr

unread,
Nov 1, 2011, 2:45:21 PM11/1/11
to CK, ggplot2
Can you use dput() instead. I've been unable to read it in using the methods I know. 

Brandon

CK

unread,
Nov 1, 2011, 3:58:01 PM11/1/11
to ggplot2
Sure.

structure(list(Gene_Symbol = structure(c(4L, 2L, 3L, 12L, 6L,
1L, 7L, 10L, 8L, 9L, 5L, 11L, 4L, 2L, 3L, 12L, 6L, 1L, 7L, 10L,
8L, 9L, 5L, 11L, 4L, 2L, 3L, 12L, 6L, 1L, 7L, 10L, 8L, 9L, 5L,
11L, 4L, 2L, 3L, 12L, 6L, 1L, 7L, 10L, 8L, 9L, 5L, 11L), .Label =
c("1300010F03Rik",
"Acsbg1", "Acsl3", "Angptl4", "AU018091", "Bnip3", "Cpt1a", "Fgfbp1",
"Lpcat3", "Scd1", "Slc25a20", "Vnn1"), class = "factor"), variable =
c("a",
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "b", "b",
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "c", "c", "c",
"c", "c", "c", "c", "c", "c", "c", "c", "c", "d", "d", "d", "d",
"d", "d", "d", "d", "d", "d", "d", "d"), value = c(1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 17.6813585102252, 3.23071906944177,
2.09114873595659,
1.47224148183269, 1.56854362291394, 1.51226627145813,
2.45857362657405,
1.50149557861824, 1.53984001699989, 1.63901833877644,
3.20941747895525,
1.72643866703735, 3.50881064670223, 1.60354399360629,
1.47455592016899,
1.32082251130455, 1.62811856958207, 1.39708692575549,
1.5092564116014,
1.31564197567378, 1.58600261563202, 1.36521678761718,
1.46924525254286,
1.41594780064409, 3.67185999917645, 1.41953210273859,
1.41537898173767,
1.32873369521246, 1.65454269790977, 1.35625807855527,
1.44201334571711,
1.34972111898344, 1.32068746493222, 1.36647137016739,
1.49519247145254,
1.31564843552276)), .Names = c("Gene_Symbol", "variable", "value"
), row.names = c(NA, 48L), class = "data.frame")

Brandon Hurr

unread,
Nov 1, 2011, 5:08:23 PM11/1/11
to CK, ggplot2
It's not perfect, but is 
ggplot(z, aes(variable, Gene_Symbol)) +
geom_tile(aes(fill=value)) +
scale_fill_gradientn(colour = c("darkgreen", "white", "white", "white", "darkred"), breaks= c(-5, -1.3, 0, 1.3, 5))

Better?

Brandon

CK

unread,
Nov 1, 2011, 6:03:58 PM11/1/11
to ggplot2
It's an improvement. For this particular subset (dput posted above),
there are no values <1.3 and therefore no tiles should be green.

Group 'a' (all the values = 1) should be completely white and groups
b,c, and d ought to be differing shades of red rising to a peak red
intensity at 5 (only one sample exceeds this in this example).

I should be able to later rbind in other data with any other values
(including negatives) and the colour scale needs to remain the same.

Does scale_fill_gradientn take into account the data values when
producing a gradient? It seems the large value of the outlier has
skewed the scale (on my screen it's the only tile that is red).

CK

unread,
Nov 1, 2011, 6:15:04 PM11/1/11
to ggplot2
Removing the outlier produces a completely different result.

This seems to confirm that scale_gradientn() creates a gradient
dynamically based at least in part on the range of data values it is
plotting.

Brandon Hurr

unread,
Nov 1, 2011, 6:17:47 PM11/1/11
to CK, ggplot2
In other words, it's ignoring your breaks. It shouldn't be doing that as far as I know. Perhaps Hadley or Takahashi know why?

B

Brian Diggs

unread,
Nov 1, 2011, 7:29:03 PM11/1/11
to ggp...@googlegroups.com
ggplot(z, aes(variable, Gene_Symbol)) +
geom_tile(aes(fill=value)) +
scale_fill_gradientn(colour = c("darkgreen", "white", "white", "white",
"darkred"), values=c(-5, -1.5, 0, 1.3, 5), rescale=FALSE,

breaks= c(-5, -1.3, 0, 1.3, 5))


--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University

Brandon Hurr

unread,
Nov 2, 2011, 4:35:08 AM11/2/11
to Brian Diggs, ggp...@googlegroups.com
Brian, 

Thanks, didn't know about that feature, although, why, if you're defining your breaks, is it rescaling in the first place?

Brandon


More options:http://groups.google.com/group/ggplot2

--
You received this message because you are subscribed to the
ggplot2
mailing list.
Please provide a reproducible example:
http://gist.github.com/270442


--
You received this message because you are subscribed to the
ggplot2
mailing list.
Please provide a reproducible example:
http://gist.github.com/270442

--
You received this message because you are subscribed to the ggplot2
mailing list.
Please provide a reproducible example:http://gist.github.com/270442


--
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
--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University
--
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

Brian Diggs

unread,
Nov 3, 2011, 11:36:42 AM11/3/11
to ggplot2
On 11/2/2011 1:35 AM, Brandon Hurr wrote:
> Brian,
>
> Thanks, didn't know about that feature, although, why, if you're defining
> your breaks, is it rescaling in the first place?

Breaks define which values are displayed on the scale but, in principle,
should not affect the mapping between data and aesthetic (the displayed
scale being the graphical representation of the reverse mapping from
aesthetic to data). I think.

I figured this out looking at the examples at
http://had.co.nz/ggplot2/scale_gradientn.html

> Brandon


>
> On Tue, Nov 1, 2011 at 23:29, Brian Diggs<diggsb-k1...@public.gmane.org> wrote:
>
>> ggplot(z, aes(variable, Gene_Symbol)) +
>> geom_tile(aes(fill=value)) +
>> scale_fill_gradientn(colour = c("darkgreen", "white", "white", "white",
>> "darkred"), values=c(-5, -1.5, 0, 1.3, 5), rescale=FALSE,
>>
>> breaks= c(-5, -1.3, 0, 1.3, 5))
>>
>> On 11/1/2011 3:17 PM, Brandon Hurr wrote:
>>
>>> In other words, it's ignoring your breaks. It shouldn't be doing that as
>>> far as I know. Perhaps Hadley or Takahashi know why?
>>>
>>> B
>>>

>>>>> On Tue, Nov 1, 2011 at 01:45, CK<combizkhoz...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>


>>>>>>>>>>
>>>>>>>>> wrote:
>>>>
>>>>> Hi thanks for your reply. I've taken a look at
>>>>>>>>>>>
>>>>>>>>>> scale_gradientn()
>>>>
>>>>> and
>>>>>>>
>>>>>>>> it looks promising but I'm not quite getting the results I
>>>>>>>>>>>
>>>>>>>>>> would
>>>>
>>>>> like
>>>>>>>
>>>>>>>> yet. Here's what I have: -
>>>>>>>>>>>
>>>>>>>>>>
>>>>> breaks = c(-5, -1.3, 0, 1.3, 5)
>>>>>>>>>>> mycolours = c("green", "grey50", "white", "grey50", "red")
>>>>>>>>>>>
>>>>>>>>>>
>>>>> p<- ggplot(z, aes(variable, Gene_Symbol)) +
>>>>>>>>>>> geom_tile(aes(fill=value)) +
>>>>>>>>>>> scale_fill_gradientn(colour = mycolours, breaks=breaks,
>>>>>>>>>>> labels=format(breaks))
>>>>>>>>>>> p
>>>>>>>>>>>
>>>>>>>>>>
>>>>> The result is a heatmap that appears mostly green, not quite
>>>>>>>>>>>
>>>>>>>>>> what
>>>>
>>>>> was
>>>>>>>
>>>>>>>> intended.
>>>>>>>>>>>
>>>>>>>>>>
>>>>> Thanks for any suggestions.
>>>>>>>>>>>
>>>>>>>>>>

>>>>> On Oct 29, 10:00 am, Brandon Hurr<brandon.h...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>


>>>>>>>>>>>
>>>>>>>>>> wrote:
>>>>
>>>>> I think you might be able to get what you want with
>>>>>>>>>>>>
>>>>>>>>>>> scale_gradientn().
>>>>>>>>>
>>>>>>>>>> You
>>>>>>>>>>>
>>>>>>>>>>>> can specify more breaks that way and keep the middle of
>>>>>>>>>>>>
>>>>>>>>>>> your data
>>>>
>>>>> more
>>>>>>>>>
>>>>>>>>>> white.
>>>>>>>>>>>>
>>>>>>>>>>>

>>>>> On Fri, Oct 28, 2011 at 23:29, CK<combizkhoz...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org


>>>>>>>>>>>>
>>>>>>>>>>>
>>>>> wrote:
>>>>>>>
>>>>>>>> I'm trying to produce a heatmap of values with the
>>>>>>>>>>>>>
>>>>>>>>>>>> following
>>>>
>>>>> color
>>>>>>>
>>>>>>>> gradient ranges: -
>>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>> Low, -1.3 to -5, increasing green peaking at -5
>>>>>>>>>>>>> Mid,>-1.3 to<1.3, White
>>>>>>>>>>>>> High, 1.3 to 5, increasing red peaking at 5
>>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>> The closest I can get is: -
>>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>> p<- ggplot(z, aes(variable, Gene_Symbol)) +
>>>>>>>>>>>>> geom_tile(aes(fill=value)) +

>>>>>>>>>>>>> scale_fill_gradient2(limits=c(**-5, 5), midpoint=0, low =


>>>>>>>>>>>>> muted("green"), mid="white", high = muted("red"))
>>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>> This is quite close, however all values<0 are red and
>>>>>>>>>>>>>
>>>>>>>>>>>> all
>>>>
>>>>> values
>>>>>>>
>>>>>>>> 0
>>>>>>>>>>
>>>>>>>>>>> are green. Is there a way to specify a midpoint range
>>>>>>>>>>>>>
>>>>>>>>>>>> (i.e.
>>>>
>>>>> -1.3
>>>>>>>>
>>>>>>>>> and
>>>>>>>>>
>>>>>>>>>> <1.3) rather than a single value? If not, is there any
>>>>>>>>>>>>>
>>>>>>>>>>>> other
>>>>
>>>>> way
>>>>>>>
>>>>>>>> to
>>>>>>>>>
>>>>>>>>>> get this result?
>>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>> Thanks
>>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>> --
>>>>>>>>>>>>> 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 ggplot2-/JYPxA39Uh5...@public.gmane.org
>>>>>>>>>>>>> To unsubscribe: email
>>>>>>>>>>>>>
>>>>>>>>>>>> ggplot2+unsubscribe@**googlegroups.com<ggplot2%2Bunsubscribe-/JYPxA39Uh5...@public.gmane.org>
>>>>
>>>>> More options:http://groups.google.**com/group/ggplot2<http://groups.google.com/group/ggplot2>


>>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>> --
>>>>>>>>>>> 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 ggplot2-/JYPxA39Uh5...@public.gmane.org
>>>>>>>>>>> To unsubscribe: email ggplot2+unsubscribe@**googlegroups.com<ggplot2%2Bunsubscribe-/JYPxA39Uh5...@public.gmane.org>
>>>>>>>>>>> More options:http://groups.google.**com/group/ggplot2<http://groups.google.com/group/ggplot2>


>>>>>>>>>>>
>>>>>>>>>>
>>>>> --
>>>>>>>>> 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 ggplot2-/JYPxA39Uh5...@public.gmane.org
>>>>>>>>> To unsubscribe: email ggplot2+unsubscribe@**googlegroups.com<ggplot2%2Bunsubscribe-/JYPxA39Uh5...@public.gmane.org>
>>>>>>>>> More options:http://groups.google.**com/group/ggplot2<http://groups.google.com/group/ggplot2>


>>>>>>>>>
>>>>>>>>
>>>>> --
>>>>>>> You received this message because you are subscribed to the ggplot2
>>>>>>> mailing list.

>>>>>>> Please provide a reproducible example:http://gist.github.**com/270442<http://gist.github.com/270442>
>>>>>>>
>>>>>>
>>>>> To post: email ggplot2-/JYPxA39Uh5...@public.gmane.org
>>>>>>> To unsubscribe: email ggplot2+unsubscribe@**googlegroups.com<ggplot2%2Bunsubscribe-/JYPxA39Uh5...@public.gmane.org>
>>>>>>> More options:http://groups.google.**com/group/ggplot2<http://groups.google.com/group/ggplot2>


>>>>>>>
>>>>>>
>>>> --
>>>> 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 ggplot2-/JYPxA39Uh5...@public.gmane.org
>>>> To unsubscribe: email ggplot2+unsubscribe@**googlegroups.com<ggplot2%2Bunsubscribe-/JYPxA39Uh5...@public.gmane.org>
>>>> More options: http://groups.google.com/**group/ggplot2<http://groups.google.com/group/ggplot2>


>>>>
>>>>
>>>
>>
>> --
>> Brian S. Diggs, PhD
>> Senior Research Associate, Department of Surgery

>> Oregon Health& Science University


>>
>>
>> --
>> 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 ggplot2-/JYPxA39Uh5...@public.gmane.org
>> To unsubscribe: email ggplot2+unsubscribe@**googlegroups.com<ggplot2%2Bunsubscribe-/JYPxA39Uh5...@public.gmane.org>
>> More options: http://groups.google.com/**group/ggplot2<http://groups.google.com/group/ggplot2>

CK

unread,
Nov 5, 2011, 9:46:17 PM11/5/11
to ggplot2
Thanks, that helped a lot. I found that the default behavior with
rescale=FALSE was not to display values outside the specified range.
Adding an additional extreme value at each end corrected this. Final
code that produced the intended effect was: -

ggplot(z, aes(variable, Gene_Symbol)) +
geom_tile(aes(fill=value)) +
scale_fill_gradientn(colour = c("darkgreen", "darkgreen", "white",
"white", "white", "darkred", "darkred"),
values=c(-50, -4, -1, 0, 1, 4, 50),
breaks=c(-50, -4, -1, 0, 1, 4, 50),
rescale=FALSE)

Final issue remaining is that the legend simply displays the colour
associated with each break/value, rather than rendering a true
gradient. Any thoughts on this?



On Nov 3, 11:36 am, Brian Diggs <dig...@ohsu.edu> wrote:
> On 11/2/2011 1:35 AM, Brandon Hurr wrote:
>
> > Brian,
>
> > Thanks, didn't know about that feature, although, why, if you're defining
> > your breaks, is it rescaling in the first place?
>
> Breaks define which values are displayed on the scale but, in principle,
> should not affect the mapping between data and aesthetic (the displayed
> scale being the graphical representation of the reverse mapping from
> aesthetic to data). I think.
>
> I figured this out looking at the examples athttp://had.co.nz/ggplot2/scale_gradientn.html
>
>
>
>
>
>
>
> > Brandon
>
> > On Tue, Nov 1, 2011 at 23:29, Brian Diggs<diggsb-k1yIFPgn...@public.gmane.org>  wrote:
>
> >> ggplot(z, aes(variable, Gene_Symbol)) +
> >> geom_tile(aes(fill=value)) +
> >> scale_fill_gradientn(colour = c("darkgreen", "white", "white", "white",
> >> "darkred"), values=c(-5, -1.5, 0, 1.3, 5), rescale=FALSE,
>
> >>   breaks= c(-5, -1.3, 0, 1.3, 5))
>
> >> On 11/1/2011 3:17 PM, Brandon Hurr wrote:
>
> >>> In other words, it's ignoring your breaks. It shouldn't be doing that as
> >>> far as I know. Perhaps Hadley or Takahashi know why?
>
> >>> B
>
> >>> On Tue, Nov 1, 2011 at 22:15, CK<combizkhozoie-Re5JQEeQqe8AvxtiuMw...@public.gmane.org>   wrote:
>
> >>>   Removing the outlier produces a completely different result.
>
> >>>> This seems to confirm that scale_gradientn() creates a gradient
> >>>> dynamically based at least in part on the range of data values it is
> >>>> plotting.
>
> >>>> On Nov 1, 6:03 pm, CK<combizkhoz...-Re5JQEeQqe8AvxtiuMw...@public.gmane.org>   wrote:
>
> >>>>> It's an improvement.  For this particular subset (dput posted above),
> >>>>> there are no values<1.3 and therefore no tiles should be green.
>
> >>>>> Group 'a' (all the values = 1) should be completely white and groups
> >>>>> b,c, and d ought to be differing shades of red rising to a peak red
> >>>>> intensity at 5 (only one sample exceeds this in this example).
>
> >>>>> I should be able to later rbind in other data with any other values
> >>>>> (including negatives) and the colour scale needs to remain the same.
>
> >>>>> Does scale_fill_gradientn take into account the data values when
> >>>>> producing a gradient?  It seems the large value of the outlier has
> >>>>> skewed the scale (on my screen it's the only tile that is red).
>
> >>>>> On Nov 1, 5:08 pm, Brandon Hurr<brandon.h...-Re5JQEeQqe8AvxtiuMw...@public.gmane.org>   wrote:
>
> >>>>>   It's not perfect, but is
> >>>>>> ggplot(z, aes(variable, Gene_Symbol)) +
> >>>>>> geom_tile(aes(fill=value)) +
> >>>>>> scale_fill_gradientn(colour = c("darkgreen", "white", "white", "white",
> >>>>>> "darkred"), breaks= c(-5, -1.3, 0, 1.3, 5))
>
> >>>>>   Better?
>
> >>>>>   Brandon
>
> >>>>>   On Nov 1, 2:45 pm, Brandon Hurr<brandon.h...-Re5JQEeQqe8AvxtiuMw...@public.gmane.org>   wrote:
>
> >>>>>>>> Can you use dput() instead. I've been unable to read it in using
>
> >>>>>>> the
>
> >>>>>   methods I know.
>
> >>>>>   Brandon
>
> >>>>>   On Nov 1, 4:48 am, Brandon Hurr<brandon.h...-Re5JQEeQqe8AvxtiuMw...@public.gmane.org>   wrote:
>
> >>>>>>>>>> Have you got some example data we could play with... really
>
> >>>>>>>>> hard to
>
> >>>>> go on
>
> >>>>>>>> "it's a little too green."
>
> >>>>>   Brandon
>
> >>>>>   On Tue, Nov 1, 2011 at 01:45, CK<combizkhoz...-Re5JQEeQqe8AvxtiuMw...@public.gmane.org>
>
> >>>>>>>>> wrote:
>
> >>>>>   Hi thanks for your reply.  I've taken a look at
>
> >>>>>>>>>> scale_gradientn()
>
> >>>>> and
>
> >>>>>>>>   it looks promising but I'm not quite getting the results I
>
> >>>>>>>>>> would
>
> >>>>> like
>
> >>>>>>>>   yet.  Here's what I have: -
>
> >>>>>   breaks = c(-5, -1.3, 0, 1.3, 5)
> >>>>>>>>>>> mycolours = c("green", "grey50", "white", "grey50", "red")
>
> >>>>>   p<- ggplot(z, aes(variable, Gene_Symbol)) +
> >>>>>>>>>>> geom_tile(aes(fill=value)) +
> >>>>>>>>>>> scale_fill_gradientn(colour = mycolours, breaks=breaks,
> >>>>>>>>>>> labels=format(breaks))
> >>>>>>>>>>> p
>
> >>>>>   The result is a heatmap that appears mostly green, not quite
>
> >>>>>>>>>> what
>
> >>>>> was
>
> >>>>>>>>   intended.
>
> >>>>>   Thanks for any suggestions.
>
> >>>>>   On Oct 29, 10:00 am, Brandon Hurr<brandon.h...-Re5JQEeQqe8AvxtiuMw...@public.gmane.org>
>
> >>>>>>>>>> wrote:
>
> >>>>>   I think you might be able to get what you want with
>
> >>>>>>>>>>> scale_gradientn().
>
> >>>>>>>>>> You
>
> >>>>>>>>>>>> can specify more breaks that way and keep the middle of
>
> >>>>>>>>>>> your data
>
> >>>>>   more
>
> >>>>>>>>>> white.
>
> >>>>>    On Fri, Oct 28, 2011 at 23:29, CK<combizkhoz...-Re5JQEeQqe8AvxtiuMw...@public.gmane.org
>
> >>>>>   wrote:
>
> >>>>>>>>   I'm trying to produce a heatmap of values with the
>
> >>>>>>>>>>>> following
>
> >>>>> color
>
> >>>>>>>>   gradient ranges: -
>
> >>>>>    Low, -1.3 to -5, increasing green peaking at -5
> >>>>>>>>>>>>> Mid,>-1.3 to<1.3, White
> >>>>>>>>>>>>> High, 1.3 to 5, increasing red peaking at 5
>
> >>>>>    The closest I...
>
> read more »

baptiste auguie

unread,
Nov 5, 2011, 10:14:09 PM11/5/11
to CK, ggplot2
On Sun, Nov 6, 2011 at 2:46 PM, CK <combiz...@gmail.com> wrote:
> Thanks, that helped a lot.  I found that the default behavior with
> rescale=FALSE was not to display values outside the specified range.
> Adding an additional extreme value at each end corrected this.  Final
> code that produced the intended effect was: -
>
> ggplot(z, aes(variable, Gene_Symbol)) +
> geom_tile(aes(fill=value)) +
> scale_fill_gradientn(colour = c("darkgreen", "darkgreen", "white",
> "white", "white", "darkred", "darkred"),
>                     values=c(-50, -4, -1, 0, 1, 4, 50),
>                     breaks=c(-50, -4, -1, 0, 1, 4, 50),
>                     rescale=FALSE)
>
> Final issue remaining is that the legend simply displays the colour
> associated with each break/value, rather than rendering a true
> gradient.  Any thoughts on this?
>

Kohske has been working on this,

http://kohske.wordpress.com/2011/04/01/alpha-version-of-colorbar-legend-in-ggplot2/

I'm not sure whether it's now present in the main branch of ggplot2.

baptiste

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>

Kohske Takahashi

unread,
Nov 6, 2011, 1:24:40 AM11/6/11
to baptiste auguie, CK, ggplot2
Hi,

not sure but maybe you can do like this in the next version:

ggplot(z, aes(variable, Gene_Symbol)) +
geom_tile(aes(fill=value)) +

scale_fill_gradientn(colours = c("darkgreen", "darkgreen",


"white","white", "white", "darkred", "darkred"),
                   values=c(-50, -4, -1, 0, 1, 4, 50),

                   rescale = function(x, ...) x, oob = identity) +
                   guides(fill = guide_colorbar(barheight = 10))


--
Kohske Takahashi <takahash...@gmail.com>

Research Center for Advanced Science and Technology,
The University of  Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

2011/11/6 baptiste auguie <bapt...@googlemail.com>:

plot.png

CK

unread,
Nov 6, 2011, 1:41:21 AM11/6/11
to ggplot2
Kohske, that legend gradient is indeed a big improvement. I noticed it
isn't showing the negative (green) values though?

On Nov 6, 1:24 am, Kohske Takahashi <takahashi.koh...@gmail.com>
wrote:
> Hi,
>
> not sure but maybe you can do like this in the next version:
>
> ggplot(z, aes(variable, Gene_Symbol)) +
> geom_tile(aes(fill=value)) +
> scale_fill_gradientn(colours = c("darkgreen", "darkgreen",
> "white","white", "white", "darkred", "darkred"),
>                    values=c(-50, -4, -1, 0, 1, 4, 50),
>                    rescale = function(x, ...) x, oob = identity) +
>                    guides(fill = guide_colorbar(barheight = 10))
>
> --
> Kohske Takahashi <takahashi.koh...@gmail.com>
>
> Research Center for Advanced Science and Technology,
> The University of  Tokyo, Japan.http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html
>
> 2011/11/6 baptiste auguie <bapt4...@googlemail.com>:
>
>
>
>
>
>
>
> > On Sun, Nov 6, 2011 at 2:46 PM, CK <combizkhoz...@gmail.com> wrote:
> >> Thanks, that helped a lot.  I found that the default behavior with
> >> rescale=FALSE was not to display values outside the specified range.
> >> Adding an additional extreme value at each end corrected this.  Final
> >> code that produced the intended effect was: -
>
> >> ggplot(z, aes(variable, Gene_Symbol)) +
> >> geom_tile(aes(fill=value)) +
> >> scale_fill_gradientn(colour = c("darkgreen", "darkgreen", "white",
> >> "white", "white", "darkred", "darkred"),
> >>                     values=c(-50, -4, -1, 0, 1, 4, 50),
> >>                     breaks=c(-50, -4, -1, 0, 1, 4, 50),
> >>                     rescale=FALSE)
>
> >> Final issue remaining is that the legend simply displays the colour
> >> associated with each break/value, rather than rendering a true
> >> gradient.  Any thoughts on this?
>
> > Kohske has been working on this,
>
> >http://kohske.wordpress.com/2011/04/01/alpha-version-of-colorbar-lege...
> ...
>
> read more »
>
>  plot.png
> 62KViewDownload

Kohske Takahashi

unread,
Nov 6, 2011, 1:48:03 AM11/6/11
to CK, ggplot2
Hi,

2011/11/6 CK <combiz...@gmail.com>:


> Kohske, that legend gradient is indeed a big improvement. I noticed it
> isn't showing the negative (green) values though?

this is because of the range of data.
z$value only include > 1.
If data ranges more broadly, then it looks like this:

z$value <- runif(nrow(z), -20, 20)

plot2.png

CK

unread,
Nov 6, 2011, 1:52:49 AM11/6/11
to ggplot2
Nice, looks perfect. Is it possible to access this before the next
release? I'm on Windows.

On Nov 6, 1:48 am, Kohske Takahashi <takahashi.koh...@gmail.com>
wrote:
> Hi,
>
> 2011/11/6 CK <combizkhoz...@gmail.com>:
> >> >>> >>>>>>>>> "27"    "Acsl3" "c"    ...
>
> read more »
>
>  plot2.png
> 62KViewDownload

Kohske Takahashi

unread,
Nov 6, 2011, 1:09:17 AM11/6/11
to CK, ggplot2
> library(devtools)> dev_mode(TRUE, path="~/R-dev/tmp")Dev mode: ON> install_github("ggplot2", "kohske", "cutting-edge")
> library(ggplot2)

will install and load ggplot2 on the temporary directory.
I'm not sure if this works well on Windows.

--
Kohske Takahashi <takahash...@gmail.com>

Research Center for Advanced Science and Technology,
The University of  Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

2011/11/6 CK <combiz...@gmail.com>:

CK

unread,
Nov 6, 2011, 1:49:43 PM11/6/11
to ggplot2
Thanks. I was unable to get it working in windows unfortunately. I
will have to make the gradient legend by hand in Illustrator/
Photoshop.

In the end I used the alpha setting to toggle visibility (1 or 0)
based on whether the value passed the -1.3 or +1.3 cutoff. i.e.

falphaSet <- function(value){
cutoff = 1.3
if (value < cutoff & value > -cutoff) {aValue = 0} # invisible
else {aValue = 1} # visible
return(aValue)
}

z$aValue = sapply(z$value, "falphaSet")

ggplot(z, aes(variable, Gene_Symbol)) +
geom_tile(aes(fill=value, alpha=aValue)) +
etc.

Thanks for all the input =)


On Nov 6, 1:09 am, Kohske Takahashi <takahashi.koh...@gmail.com>
wrote:
> > library(devtools)> dev_mode(TRUE, path="~/R-dev/tmp")Dev mode: ON> install_github("ggplot2", "kohske", "cutting-edge")
> > library(ggplot2)
>
> will install and load ggplot2 on the temporary directory.
> I'm not sure if this works well on Windows.
>
> --
> Kohske Takahashi <takahashi.koh...@gmail.com>
>
> Research Center for Advanced Science and Technology,
> The University of  Tokyo, Japan.http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html
>
> 2011/11/6 CK <combizkhoz...@gmail.com>:
> >> >> >>> >>>>>>>>> "10"    "Lpcat3"  ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages