How to generate a vector with "=" assignments, from two vectors, for scale_colour_manual

86 views
Skip to first unread message

Sujai

unread,
Apr 18, 2012, 6:39:38 AM4/18/12
to ggp...@googlegroups.com
I just submitted this to biostars, but thought it might be better to ask here: http://www.biostars.org/post/show/42988/generate-vector-with-assignments-from-two-vectors/

The ggplot2 manual at http://had.co.nz/ggplot2/scale_manual.html suggests using a structure like:

 values = c("8" = "red","4" = "blue","6" = "darkgreen", "10" = "orange")

to assign specific colours to values.

How does one generate this structure from two arrays:

A = c("8","4","6","10"); B = c("red","blue","darkgreen","orange")

I got as far as:

rbind(A,B)
  [,1]  [,2]   [,3]        [,4]    
A "8"   "4"    "6"         "10"    
B "red" "blue" "darkgreen" "orange"

But I am not sure how to turn that into an array of "X" = "Y" assignments.

Sujai

unread,
Apr 18, 2012, 7:31:38 AM4/18/12
to ggp...@googlegroups.com
ps. in case any one clicks on the biostars link, I deleted it from there, and put the question on stackoverflow.com instead because it is not a biology specific question: http://stackoverflow.com/questions/10208378/generate-a-vector-in-r-with-assignments-from-two-linear-vectors-for-scale

Jean-Olivier Irisson

unread,
Apr 18, 2012, 7:47:17 AM4/18/12
to Sujai, ggplot2
On 2012-Apr-18, at 12:39 , Sujai wrote:
>
> The ggplot2 manual at http://had.co.nz/ggplot2/scale_manual.html suggests using a structure like:
>
> values = c("8" = "red","4" = "blue","6" = "darkgreen", "10" = "orange")
> to assign specific colours to values.
>
> How does one generate this structure from two arrays:
>
> A = c("8","4","6","10"); B = c("red","blue","darkgreen","orange")

names(B) <- A

But in this particular case I would rather use

scale_colour_manual(breaks=A, values=B)

Jean-Olivier Irisson
---
Observatoire Océanologique
Station Zoologique, B.P. 28, Chemin du Lazaret
06230 Villefranche-sur-Mer
Tel: +33 04 93 76 38 04
Mob: +33 06 21 05 19 90
http://jo.irisson.com/

Sujai

unread,
Apr 18, 2012, 8:02:36 AM4/18/12
to Jean-Olivier Irisson, ggplot2
Hi Jean-Olivier

> How does one generate this structure from two arrays:
>
> A = c("8","4","6","10"); B = c("red","blue","darkgreen","orange")

       names(B) <- A

Great! This worked perfectly. Thanks very very much. I hadn't realised that c("8" = "red","4" = "blue","6" = "darkgreen", "10" = "orange") was the same as a named vector. Obviously, I still am not very familiar with R.
 
But in this particular case I would rather use

       scale_colour_manual(breaks=A, values=B)

I had tried this already. It seemed to only reorder the legend items. The colours were not being reordered as expected.

Thanks again!

Jean-Olivier Irisson

unread,
Apr 18, 2012, 8:10:36 AM4/18/12
to Sujai, ggplot2
On 2012-Apr-18, at 14:02 , Sujai wrote:
>
> But in this particular case I would rather use
>
> scale_colour_manual(breaks=A, values=B)
>
> I had tried this already. It seemed to only reorder the legend items. The colours were not being reordered as expected.

If you keep the same order in the breaks and the colours, it should give you what you want, i.e.

scale_colour_manual(breaks=c(1,2,3), values=c("red", "green", "blue"))

scale_colour_manual(breaks=c(3,2,1), values=c("blue" , "green","red"))

scale_colour_manual(breaks=c(1,3,2), values=c("red", "blue", "green"))

give the same graphic, only the legend key is changed.

Jean-Olivier Irisson

unread,
Apr 18, 2012, 8:16:04 AM4/18/12
to Sujai, ggplot2
On 2012-Apr-18, at 14:10 , Jean-Olivier Irisson wrote:
>
> If you keep the same order in the breaks and the colours, it should give you what you want, i.e.
>
> scale_colour_manual(breaks=c(1,2,3), values=c("red", "green", "blue"))
>
> scale_colour_manual(breaks=c(3,2,1), values=c("blue" , "green","red"))
>
> scale_colour_manual(breaks=c(1,3,2), values=c("red", "blue", "green"))
>
> give the same graphic, only the legend key is changed.

Ooops no it does not. Sorry, the only way to do it is with a named vector indeed.

That's strange tough, it would seem natural to do it this way. Hadley?

Hadley Wickham

unread,
Apr 18, 2012, 9:02:45 AM4/18/12
to Sujai, ggp...@googlegroups.com
On Wed, Apr 18, 2012 at 5:39 AM, Sujai <sujai...@gmail.com> wrote:
> I just submitted this to biostars, but thought it might be better to ask
> here:
> http://www.biostars.org/post/show/42988/generate-vector-with-assignments-from-two-vectors/
>
> The ggplot2 manual at http://had.co.nz/ggplot2/scale_manual.html suggests
> using a structure like:
>
> values = c("8" = "red","4" = "blue","6" = "darkgreen", "10" = "orange")
>
> to assign specific colours to values.
>
> How does one generate this structure from two arrays:
>
> A = c("8","4","6","10"); B = c("red","blue","darkgreen","orange")

setNames(A, B)

Hadley

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

Hadley Wickham

unread,
Apr 18, 2012, 9:03:52 AM4/18/12
to Jean-Olivier Irisson, Sujai, ggplot2
On Wed, Apr 18, 2012 at 7:16 AM, Jean-Olivier Irisson
<iri...@normalesup.org> wrote:
> On 2012-Apr-18, at 14:10 , Jean-Olivier Irisson wrote:
>>
>> If you keep the same order in the breaks and the colours, it should give you what you want, i.e.
>>
>>       scale_colour_manual(breaks=c(1,2,3), values=c("red", "green", "blue"))
>>
>>       scale_colour_manual(breaks=c(3,2,1), values=c("blue" , "green","red"))
>>
>>       scale_colour_manual(breaks=c(1,3,2), values=c("red", "blue", "green"))
>>
>> give the same graphic, only the legend key is changed.
>
> Ooops no it does not. Sorry, the only way to do it is with a named vector indeed.
>
> That's strange tough, it would seem natural to do it this way. Hadley?

Well, a lot of things seems natural, but it's not always possible. I
think there was also a bug that's been fixed in the devel version that
may be relevant.

Winston Chang

unread,
Apr 18, 2012, 11:27:12 AM4/18/12
to Hadley Wickham, Jean-Olivier Irisson, Sujai, ggplot2
It looks like a related bug was fixed in https://github.com/hadley/ggplot2/issues/264

The fix made this work:
qplot(mpg, wt, data = mtcars, colour = factor(cyl)) + scale_colour_manual(values = c("8" = "red","4" = "blue","6" = "green"))

... but it didn't make this work:
p + scale_colour_manual(breaks = c(8,6,4), values = c("red","blue","green"))

-Winston



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

Winston Chang

unread,
Apr 19, 2012, 1:10:41 PM4/19/12
to Hadley Wickham, Jean-Olivier Irisson, Sujai, ggplot2
Sorry, my mistake. I mixed up breaks and limits. For a discrete scale, you need to set limits, not breaks.

# Works
qplot(mpg, wt, data = mtcars, colour = factor(cyl)) + scale_colour_manual(limits = c(8,6,4), values = c("red","blue","green"))

# Doesn't work
qplot(mpg, wt, data = mtcars, colour = factor(cyl)) + scale_colour_manual(breaks = c(8,6,4), values = c("red","blue","green"))

Sujai

unread,
Apr 19, 2012, 4:08:12 PM4/19/12
to ggp...@googlegroups.com
A=c(8,6,4)
B=c("red","blue","green")

Thanks all. All three worked great:

1. Jean-Olivier
names(B)<-A

2. Hadley
setNames(A,B)

3. Winston
scale_colour_manual(limits = A, values = B)

Much appreciated!

Jean-Olivier Irisson

unread,
Apr 19, 2012, 4:58:56 PM4/19/12
to Winston Chang, ggplot
On 2012-Apr-19, at 19:10 , Winston Chang wrote:
>
> Sorry, my mistake. I mixed up breaks and limits. For a discrete scale, you need to set limits, not breaks.
>
> # Works
> qplot(mpg, wt, data = mtcars, colour = factor(cyl)) + scale_colour_manual(limits = c(8,6,4), values = c("red","blue","green"))

That's very strange. What are breaks for then?

Winston Chang

unread,
Apr 19, 2012, 5:09:43 PM4/19/12
to Jean-Olivier Irisson, ggplot
The breaks control what shows up on the guide. In this example, only 8 and 4 are in the legend:

qplot(mpg, wt, data = mtcars, colour = factor(cyl)) + 
  scale_colour_manual(limits = c(8,6,4), values = c("red","blue","green"), breaks=c(8,4))

I agree that it can be confusing...

Hadley Wickham

unread,
Apr 19, 2012, 6:26:35 PM4/19/12
to Jean-Olivier Irisson, Winston Chang, ggplot
>> # Works
>> qplot(mpg, wt, data = mtcars, colour = factor(cyl)) + scale_colour_manual(limits = c(8,6,4), values = c("red","blue","green"))
>
> That's very strange. What are breaks for then?

Controlling the legend (the display, not the underlying mapping).

Reply all
Reply to author
Forward
0 new messages