ggplot2 - legend for fill coulours

View: New views
5 Messages — Rating Filter:   Alert me  

ggplot2 - legend for fill coulours

by pbarros :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Dear All,

I am trying to build a stacked bar plot, where I define the colours to use.

I have asked this before, and I was using a solution in
http://thread.gmane.org/gmane.comp.lang.r.general/100649/focus=100673 
(thanks, Thierry).

However, it looks this works only when the data are in the sequence
of the levels in the factor defining the fill colours. When the
sequence is different, the legend gets "scrambled", in that the order
of the colours does not match the labels.

My code is below. Can anyone tell me how to get around this? (R 2.2.6
for Windows, ggplot2 version 2_0.5.7)

Thanks,
Pedro
library(ggplot2)
Data:

 > plotdata2
    x   y  group
1  1 0.1 grey30
2  2 0.2 grey30
3  3 0.3 grey10
4  4 0.4 grey90
5  1 0.1 grey30
6  2 0.2 grey60
7  3 0.3 grey60
8  4 0.4 grey90
9  1 0.1 grey60
10 2 0.2 grey10
11 3 0.3 grey90
12 4 0.4 grey30
13 1 0.1 grey90
14 2 0.2 grey60
15 3 0.3 grey10
16 4 0.4 grey10
 > levels(plotdata2$group)
[1] "grey10" "grey30" "grey60" "grey90"
 > as.numeric(plotdata2$group)
  [1] 2 2 1 4 2 3 3 4 3 1 4 2 4 3 1 1

Code:
plot0<-ggplot()
layer1<-layer(data=plotdata2, mapping=aes_string(x='x',y='y',
fill='group'),geom='bar', stat='identity', position='stack')
scaleFill<-scale_fill_identity(labels=levels(plotdata2$group),
guide='tile', name='Group')
scaleY<-scale_y_continuous(limits=c(0,4), expand=c(0,0))
plot1<-plot0+layer1 +scaleFill +scaleY

plot1

______________________________________________
R-help@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Re: ggplot2 - legend for fill coulours

by ONKELINX, Thierry :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Dear Pedro,

Sorting your data before plotting it will probably sort your problem.

plotdata2 <- plotdata2[order(plotdata2$group), ]

HTH,

Thierry

------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature
and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium
tel. + 32 54/436 185
Thierry.Onkelinx@...
www.inbo.be

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
data.
~ John Tukey

-----Oorspronkelijk bericht-----
Van: r-help-bounces@... [mailto:r-help-bounces@...]
Namens Pedro de Barros
Verzonden: zondag 23 maart 2008 13:02
Aan: r-help@...
Onderwerp: [R] ggplot2 - legend for fill coulours
Urgentie: Hoog

Dear All,

I am trying to build a stacked bar plot, where I define the colours to
use.

I have asked this before, and I was using a solution in
http://thread.gmane.org/gmane.comp.lang.r.general/100649/focus=100673 
(thanks, Thierry).

However, it looks this works only when the data are in the sequence
of the levels in the factor defining the fill colours. When the
sequence is different, the legend gets "scrambled", in that the order
of the colours does not match the labels.

My code is below. Can anyone tell me how to get around this? (R 2.2.6
for Windows, ggplot2 version 2_0.5.7)

Thanks,
Pedro
library(ggplot2)
Data:

 > plotdata2
    x   y  group
1  1 0.1 grey30
2  2 0.2 grey30
3  3 0.3 grey10
4  4 0.4 grey90
5  1 0.1 grey30
6  2 0.2 grey60
7  3 0.3 grey60
8  4 0.4 grey90
9  1 0.1 grey60
10 2 0.2 grey10
11 3 0.3 grey90
12 4 0.4 grey30
13 1 0.1 grey90
14 2 0.2 grey60
15 3 0.3 grey10
16 4 0.4 grey10
 > levels(plotdata2$group)
[1] "grey10" "grey30" "grey60" "grey90"
 > as.numeric(plotdata2$group)
  [1] 2 2 1 4 2 3 3 4 3 1 4 2 4 3 1 1

Code:
plot0<-ggplot()
layer1<-layer(data=plotdata2, mapping=aes_string(x='x',y='y',
fill='group'),geom='bar', stat='identity', position='stack')
scaleFill<-scale_fill_identity(labels=levels(plotdata2$group),
guide='tile', name='Group')
scaleY<-scale_y_continuous(limits=c(0,4), expand=c(0,0))
plot1<-plot0+layer1 +scaleFill +scaleY

plot1

______________________________________________
R-help@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Re: ggplot2 - legend for fill coulours

by pbarros :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Dear Thierry,

Thanks, it worked. I though the algorithm behind the plotting uses
some approach like unique(), so that the order of the plot is that of
the first occurrence of each code, but I could not get into the code
to modify this.
Anyway, Hadley has already gently volunteered to look into this for a
more permanent solution, I look forward to it, as I really like the
ggplot philosophy...

Thanks again,

Pedro
At 09:24 2008/03/25, you wrote:

>Dear Pedro,
>
>Sorting your data before plotting it will probably sort your problem.
>
>plotdata2 <- plotdata2[order(plotdata2$group), ]
>
>HTH,
>
>Thierry
>
>------------------------------------------------------------------------
>----
>ir. Thierry Onkelinx
>Instituut voor natuur- en bosonderzoek / Research Institute for Nature
>and Forest
>Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
>methodology and quality assurance
>Gaverstraat 4
>9500 Geraardsbergen
>Belgium
>tel. + 32 54/436 185
>Thierry.Onkelinx@...
>www.inbo.be
>
>To call in the statistician after the experiment is done may be no more
>than asking him to perform a post-mortem examination: he may be able to
>say what the experiment died of.
>~ Sir Ronald Aylmer Fisher
>
>The plural of anecdote is not data.
>~ Roger Brinner
>
>The combination of some data and an aching desire for an answer does not
>ensure that a reasonable answer can be extracted from a given body of
>data.
>~ John Tukey
>
>-----Oorspronkelijk bericht-----
>Van: r-help-bounces@... [mailto:r-help-bounces@...]
>Namens Pedro de Barros
>Verzonden: zondag 23 maart 2008 13:02
>Aan: r-help@...
>Onderwerp: [R] ggplot2 - legend for fill coulours
>Urgentie: Hoog
>
>Dear All,
>
>I am trying to build a stacked bar plot, where I define the colours to
>use.
>
>I have asked this before, and I was using a solution in
>http://thread.gmane.org/gmane.comp.lang.r.general/100649/focus=100673
>(thanks, Thierry).
>
>However, it looks this works only when the data are in the sequence
>of the levels in the factor defining the fill colours. When the
>sequence is different, the legend gets "scrambled", in that the order
>of the colours does not match the labels.
>
>My code is below. Can anyone tell me how to get around this? (R 2.2.6
>for Windows, ggplot2 version 2_0.5.7)
>
>Thanks,
>Pedro
>library(ggplot2)
>Data:
>
>  > plotdata2
>     x   y  group
>1  1 0.1 grey30
>2  2 0.2 grey30
>3  3 0.3 grey10
>4  4 0.4 grey90
>5  1 0.1 grey30
>6  2 0.2 grey60
>7  3 0.3 grey60
>8  4 0.4 grey90
>9  1 0.1 grey60
>10 2 0.2 grey10
>11 3 0.3 grey90
>12 4 0.4 grey30
>13 1 0.1 grey90
>14 2 0.2 grey60
>15 3 0.3 grey10
>16 4 0.4 grey10
>  > levels(plotdata2$group)
>[1] "grey10" "grey30" "grey60" "grey90"
>  > as.numeric(plotdata2$group)
>   [1] 2 2 1 4 2 3 3 4 3 1 4 2 4 3 1 1
>
>Code:
>plot0<-ggplot()
>layer1<-layer(data=plotdata2, mapping=aes_string(x='x',y='y',
>fill='group'),geom='bar', stat='identity', position='stack')
>scaleFill<-scale_fill_identity(labels=levels(plotdata2$group),
>guide='tile', name='Group')
>scaleY<-scale_y_continuous(limits=c(0,4), expand=c(0,0))
>plot1<-plot0+layer1 +scaleFill +scaleY
>
>plot1
>
>______________________________________________
>R-help@... mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Re: ggplot2 - legend for fill coulours

by hadley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

>  However, it looks this works only when the data are in the sequence
>  of the levels in the factor defining the fill colours. When the
>  sequence is different, the legend gets "scrambled", in that the order
>  of the colours does not match the labels.

You can fix this by explicitly specifying the breaks to use in the
identity scale:

 + scale_fill_identity(labels=levels(plotdata2$group),
breaks=levels(plotdata2$group))

That way there's no confusion about how the values and their labels match up.

Regards,

Hadley

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

______________________________________________
R-help@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Parent Message unknown Re: ggplot2 - legend for fill coulours

by pbarros :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hadley,

Thanks a lot!

BTW, do you have any document explaining the object philosophy of
ggplot? I was trying to see how you defined scale_fill_identity, but
could not find it...

Cheers,
Pedro
At 19:50 2008/04/01, hadley wickham wrote:

> >  However, it looks this works only when the data are in the sequence
> >  of the levels in the factor defining the fill colours. When the
> >  sequence is different, the legend gets "scrambled", in that the order
> >  of the colours does not match the labels.
>
>You can fix this by explicitly specifying the breaks to use in the
>identity scale:
>
>  + scale_fill_identity(labels=levels(plotdata2$group),
>breaks=levels(plotdata2$group))
>
>That way there's no confusion about how the values and their labels match up.
>
>Regards,
>
>Hadley
>
>--
>http://had.co.nz/

______________________________________________
R-help@... mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.