consolidate legends in ggplot2

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

consolidate legends in ggplot2

Engelmann, Bernd
Hello,

the same parameter for colour and shape aesthetics gives 2 legends:

library(ggplot2)
p <- ggplot(mtcars, aes(x=wt, y=mpg))
p + geom_point(aes(colour=factor(cyl), shape=factor(cyl)))

Can the 2 legends be consolidated to one with
colours and shapes of the symbols matched between legend and plot ?

Thank you

Bernd Engelmann
[hidden email]
phone: + 49-351-277-4545  
fax: + 49-351-277-9-4545
AMD Saxony/AMD Saxony Limited Liability Company & Co. KG
M/S E23-TY
Wilschdorfer Landstr. 101
D-01109 Dresden
Germany

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: consolidate legends in ggplot2

hadley wickham
On Feb 7, 2008 3:43 AM, Engelmann, Bernd <[hidden email]> wrote:

> Hello,
>
> the same parameter for colour and shape aesthetics gives 2 legends:
>
> library(ggplot2)
> p <- ggplot(mtcars, aes(x=wt, y=mpg))
> p + geom_point(aes(colour=factor(cyl), shape=factor(cyl)))
>
> Can the 2 legends be consolidated to one with
> colours and shapes of the symbols matched between legend and plot ?

Not in the current version of ggplot2, but I'm working on that code in
the development version at the moment and it will be included in the
next version.  If your real example is more complicated than that,
please let me know so that I can check if my code works.

Hadley

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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: consolidate legends in ggplot2

Engelmann, Bernd
Thank you, Hadley,

the real example needs scales with more breaks, this is the only
difference.

The script overwrites the original ggplot2 code with the code below to
achive this.
This was hardcoded for the special case.

Can ggplot2 handle bigger numbers of breaks by reusing aesthetics ?

Bernd


ScaleMy <- proto(ScaleColour, expr={

        new <- function(., name=NULL, palette=1, type="qual", alpha=1,
variable) {
                proto(., name=name, palette=palette, type=type,
.input=variable, .output=variable, .alpha=alpha)
        }
       
  colors <-
c('green','red','blue','violet','orange2','skyblue','magenta','burlywood
1',
 
'cadetblue1','maroon3','aquamarine','bisque1','darkgoldenrod1','darkkhak
i','cornsilk3',
  'lightsalmon1','deepskyblue2','goldenrod1','hotpink','lightslateblue',
  'mistyrose1','olivedrab1','plum1','pink1','orange3')

        breaks <- function(.) {
                n <- length(.$domain())
                alpha(.$colors[1:n
   
    ], .$.alpha)
        }

       
        max_levels <- function(.) length(.$colors)


})

scale_colour_My <- function(...) ScaleMy$new(..., variable = "colour")


Scales$guide_legend <- function(.) {
                legs <- compact(lapply(.$.scales, function(x)
x$guide_legend()))
                legs[sapply(legs, length) == 0] <- NULL
                legs
        }

ScaleShape$breaks <- function(.) {
                (if (.$solid) {
                        c(16, 17, 15, 3, 7, 8, 16, 17, 15, 3, 7, 8, 16,
17, 15, 3, 7, 8)
                } else {
                        c(1, 2, 0, 3, 7, 8, 1, 2, 0, 3, 7, 8, 1, 2, 0,
3, 7, 8)
                })[1:length(.$domain())]
        }

ScaleShape$max_levels <- function(.) 18

 

-----Original Message-----
From: hadley wickham [mailto:[hidden email]]
Sent: Thursday, February 07, 2008 3:11 PM
To: Engelmann, Bernd
Cc: [hidden email]
Subject: Re: [R] consolidate legends in ggplot2

On Feb 7, 2008 3:43 AM, Engelmann, Bernd <[hidden email]>
wrote:

> Hello,
>
> the same parameter for colour and shape aesthetics gives 2 legends:
>
> library(ggplot2)
> p <- ggplot(mtcars, aes(x=wt, y=mpg))
> p + geom_point(aes(colour=factor(cyl), shape=factor(cyl)))
>
> Can the 2 legends be consolidated to one with colours and shapes of
> the symbols matched between legend and plot ?

Not in the current version of ggplot2, but I'm working on that code in
the development version at the moment and it will be included in the
next version.  If your real example is more complicated than that,
please let me know so that I can check if my code works.

Hadley

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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: consolidate legends in ggplot2

hadley wickham
Hi Bernd,

> Can ggplot2 handle bigger numbers of breaks by reusing aesthetics ?

No - the attributes were fairly careful picked to actually be
distinguishable, which is very hard to do above a certain number of
colours/shapes/linetypes etc.

But your approach (creating your own scales) is basically correct - I
will provide things that I think work best perceptually as defaults,
but it should be easy for you to override them if you believe
otherwise.  In the latest version you can simplify your code to:

my_colour <- scale_colour_manual(values =
c('green','red','blue','violet','orange2','skyblue','magenta','burlywood1',
'cadetblue1','maroon3','aquamarine','bisque1','darkgoldenrod1','darkkhaki',
'cornsilk3', 'lightsalmon1','deepskyblue2','goldenrod1','hotpink',
'lightslateblue', 'mistyrose1','olivedrab1','plum1','pink1','orange3'))

and

my_solid_shape <- scale_shape_manual(values = c(16, 17, 15, 3, 7, 8,
16, 17, 15, 3, 7, 8, 16, 17, 15, 3, 7, 8))
my_hollow_shape <- scale_shape_manual(values =  c(1, 2, 0, 3, 7, 8, 1,
2, 0, 3, 7, 8, 1, 2, 0, 3, 7, 8))

And the next version will actually be smart enough to create the obvious legend.

Hadley


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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: consolidate legends in ggplot2

Engelmann, Bernd
Hello Hadley,

thanks again. You are right, it normally is not a good thing to fill the
area with so many colours/shapes/linetypes, but in certain cases you
have to do it. We sometimes have more than 30 different pointclouds on a
scatterplot and the plots are still useful. These plots are analysis
workhorses not intended for the presentation of results.

Bernd

-----Original Message-----
From: hadley wickham [mailto:[hidden email]]
Sent: Thursday, February 07, 2008 8:10 PM
To: Engelmann, Bernd
Cc: [hidden email]
Subject: Re: [R] consolidate legends in ggplot2

Hi Bernd,

> Can ggplot2 handle bigger numbers of breaks by reusing aesthetics ?

No - the attributes were fairly careful picked to actually be
distinguishable, which is very hard to do above a certain number of
colours/shapes/linetypes etc.

But your approach (creating your own scales) is basically correct - I
will provide things that I think work best perceptually as defaults, but
it should be easy for you to override them if you believe otherwise.  In
the latest version you can simplify your code to:

my_colour <- scale_colour_manual(values =
c('green','red','blue','violet','orange2','skyblue','magenta','burlywood
1',
'cadetblue1','maroon3','aquamarine','bisque1','darkgoldenrod1','darkkhak
i',
'cornsilk3', 'lightsalmon1','deepskyblue2','goldenrod1','hotpink',
'lightslateblue', 'mistyrose1','olivedrab1','plum1','pink1','orange3'))

and

my_solid_shape <- scale_shape_manual(values = c(16, 17, 15, 3, 7, 8, 16,
17, 15, 3, 7, 8, 16, 17, 15, 3, 7, 8)) my_hollow_shape <-
scale_shape_manual(values =  c(1, 2, 0, 3, 7, 8, 1, 2, 0, 3, 7, 8, 1, 2,
0, 3, 7, 8))

And the next version will actually be smart enough to create the obvious
legend.

Hadley


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

______________________________________________
[hidden email] 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.