Scale fill not showing properly in legend

1,663 views
Skip to first unread message

Siebren Reker

unread,
Feb 26, 2010, 4:17:58 AM2/26/10
to ggplot2
Hello all,

I am trying to plot points with two legends, one for the shape and one
for the fill, like the code below.

pl <- ggplot()
pl <- pl + geom_point(data = mps2, aes(x=mu, y=mps2_mu, fill =
factor(beta), shape = factor(L)), size = 3) +
scale_fill_grey(start=0.4, end=0.8) + scale_shape_manual(values =
c(21,24))
pl <- pl + labs(x = "x", y="y", shape="L", fill=expression(beta))

Points are plotted properly, legend titles are fine, and the legend
for shape is fine. However, the legend for fill shows solid black
dots, not the 2 shades of grey fill I expect. Any suggestions for what
I should change?

I am using ggplot2 version 0.8.6, R version 2.9.2.

best,
Siebren Reker

Xie Chao

unread,
Feb 26, 2010, 5:30:35 AM2/26/10
to Siebren Reker, ggplot2
Although your manual shape support fill, but the legend for fill will
use the default shape for geom_point, which does not support fill ---
therefore, you will see solid points in the legend for fill.

Unless someone knows how to specify a shape for scale_fill, my
workaround would be:

ggplot(mtcars, aes(mpg, wt, shape=factor(cyl))) +
geom_point(size=3) +
# only needed if you want
# solid black edge around the dots
geom_point(aes(colour=factor(vs))) +
# faking the fill effect
scale_colour_grey(start=0.5, end=0.9)

Xie Chao

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> To post to this group, send email to ggp...@googlegroups.com
> To unsubscribe from this group, send email to
> ggplot2+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/ggplot2

Siebren Reker

unread,
Feb 26, 2010, 6:52:23 AM2/26/10
to ggplot2
Thanks for the explanation and the excellent workaround!
For anyone also using this later, I have one small addition to this
workaround:
add legend=T to the point layer for the black points, then the legend
matches the symbols in the plot more accurately.

best,
Siebren

baptiste auguie

unread,
Feb 26, 2010, 9:20:17 AM2/26/10
to ggplot2
Hi,

It seems to me that the legend should use pch=21 by default to avoid
this small bug,

GeomPoint$default_aes <- function(.) aes(shape=21, colour="black",
size=2, fill = NA, alpha = 1)

Best,

baptiste

hadley wickham

unread,
Feb 26, 2010, 11:18:12 AM2/26/10
to baptiste auguie, ggplot2
> It seems to me that the legend should use pch=21 by default to avoid
> this small bug,
>
> GeomPoint$default_aes <- function(.) aes(shape=21, colour="black",
> size=2, fill = NA, alpha = 1)

Changing the default shape aesthetic isn't a good idea because it will
make all existing scatterplots use hollow points! I think this needs
a tweak to the legend code, but no one has provided a reproducible
example :(

Hadley

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

baptiste auguie

unread,
Feb 26, 2010, 11:23:50 AM2/26/10
to hadley wickham, ggplot2
Good point, I guess there should be a test in the legend for the
specification of a fill parameter. Here's a minimal example,

ggplot(mtcars, aes(mpg, wt, shape=factor(cyl))) +

geom_point(aes(fill=factor(vs))) +
scale_fill_grey(start=0.5, end=0.9) +
scale_shape_manual(values =c(21,22,24)) +
theme_bw()

Best,

baptiste

hadley wickham

unread,
Feb 26, 2010, 11:29:29 AM2/26/10
to baptiste auguie, ggplot2
Well a minimal example would be:

ggplot(mtcars, aes(mpg, wt, shape=factor(cyl))) +
geom_point(aes(fill=factor(vs))) +

scale_shape_manual(values =c(21,22,24))

;)

But it got me on the right track and it will be fixed in the next version.

Hadley

Reply all
Reply to author
Forward
0 new messages