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

Showing 1-14 of 14 messages
How to generate a vector with "=" assignments, from two vectors, for scale_colour_manual Sujai 4/18/12 3:39 AM
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.

Re: How to generate a vector with "=" assignments, from two vectors, for scale_colour_manual Sujai 4/18/12 4:31 AM
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
Re: How to generate a vector with "=" assignments, from two vectors, for scale_colour_manual Jean-Olivier Irisson 4/18/12 4:47 AM
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/

Re: How to generate a vector with "=" assignments, from two vectors, for scale_colour_manual Sujai 4/18/12 5:02 AM
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!
Re: How to generate a vector with "=" assignments, from two vectors, for scale_colour_manual Jean-Olivier Irisson 4/18/12 5:10 AM
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
---
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/

Re: How to generate a vector with "=" assignments, from two vectors, for scale_colour_manual Jean-Olivier Irisson 4/18/12 5:16 AM
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?

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/

Re: How to generate a vector with "=" assignments, from two vectors, for scale_colour_manual Hadley Wickham 4/18/12 6:02 AM
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/

Re: How to generate a vector with "=" assignments, from two vectors, for scale_colour_manual Hadley Wickham 4/18/12 6:03 AM
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.

Hadley

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

Re: How to generate a vector with "=" assignments, from two vectors, for scale_colour_manual winston 4/18/12 8:27 AM
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

Re: How to generate a vector with "=" assignments, from two vectors, for scale_colour_manual winston 4/19/12 10:10 AM
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"))
Re: How to generate a vector with "=" assignments, from two vectors, for scale_colour_manual Sujai 4/19/12 1:08 PM
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!
Re: How to generate a vector with "=" assignments, from two vectors, for scale_colour_manual Jean-Olivier Irisson 4/19/12 1:58 PM
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?

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/

Re: How to generate a vector with "=" assignments, from two vectors, for scale_colour_manual winston 4/19/12 2:09 PM
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...
Re: How to generate a vector with "=" assignments, from two vectors, for scale_colour_manual Hadley Wickham 4/19/12 3:26 PM
>> # 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).

Hadley

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