[R] ggplot bar geom: control the filling in the colour legend

Paul Murrell p.murrell at auckland.ac.nz
Thu Sep 16 05:31:06 CEST 2010


Hi

On 13/09/2010 8:03 p.m., Benoit Boulinguiez wrote:
> Hi all,
>
> I'm still seeking for tweaking the appearance of the "color" legend in a
> bar goemetry with ggplot2.
> I can't seem to control the filling of the colour legend squares
> take this,
> ggplot(diamonds, aes(clarity, fill=color,colour = cut)) +
> geom_bar(position = "dodge")
> and I'd need a white filling in the colour legend, instead of the black one.

I couldn't see a theme element that would allow you to modify that via 
opts(), but you could go lower-level and edit the appropriate rect grobs.

Having produced the plot ...

ggplot(diamonds, aes(clarity, fill=color,colour = cut)) +
     geom_bar(position = "dodge")

... you could inspect grid.ls() output by eye to see which grobs these 
are likely to be and do something like ...

grid.gedit(gPath("GRID.cellGrob.439", "legend.frame",
                  "GRID.cellGrob", "key.gTree", "GRID.rect"),
            gp=gpar(fill="white"))

... but the "439" will change each time you produce the plot (it will 
almost certainly be a different number for you) so could automate 
somewhat by using grid.ls() to determine the *second* "GRID.cellgrob" 
directly beneath the "legends" grob ...

grobList <- grid.ls()
secondLegend <- grep("legends::GRID.cellGrob.[0-9]+$",
                      grobList$gPath)[2]
grobName <- grobList$name[secondLegend - 1]
grid.gedit(gPath(grobName, "legend.frame",
                  "GRID.cellGrob", "key.gTree", "GRID.rect"),
            gp=gpar(fill="white"))

... which should work (for your plot) every time you draw it.

Is that the result you were after?

Paul

> Does anyone know how to rule that?
>
> All the best
>
> Le 11/09/2010 10:27, Benoit Boulinguiez a écrit :
>> Sorry my bad, example too simple
>> try that one out.
>>
>> ggplot(diamonds, aes(clarity, fill=color,colour = cut)) +
>> geom_bar(position = "dodge")
>>
>> I want change the filling in the "colour" legend, not the filling of
>> the bars.
>>
>> Regards
>>
>> Le 10/09/2010 20:41, Ista Zahn a écrit :
>>> ggplot(diamonds, aes(clarity, colour = cut)) + geom_bar(fill="white")
>>>
>>
>> ______________________________________________
>> R-help at r-project.org 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 at r-project.org 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.

-- 
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/



More information about the R-help mailing list