Problem using shape and fill

1,072 views
Skip to first unread message

Mark

unread,
Nov 5, 2012, 12:27:28 PM11/5/12
to ggplot2
I can't find an explanation or work around for a problem I'm having
when I use map one variable to shape and another to fill. The below
example seems to ignore fill entirely.

ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(aes(shape=factor(vs), fill=factor(am)), size=4) +
scale_shape_discrete(solid=FALSE)

This example plots correctly but the legend is incorrect

ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(aes(shape=factor(vs), fill=factor(am)), size=4) +
scale_shape_manual(values=c(21, 23))

Brian Diggs

unread,
Nov 5, 2012, 6:55:46 PM11/5/12
to Mark, ggplot2
The solid=FALSE argument to scale_shape_discrete does not do what you
think it does; it selects solid shapes (ones that have the same middle
color as they are drawn in) rather than fillable shapes (ones which can
have a different fill color than line color.

For a catalog of all the shapes and the effects of color and fill on
them, try:

DF <- data.frame(pt = 0:25, x = 1, y = 1)
ggplot(DF, aes(x=x, y=y, shape=pt)) +
geom_point(colour="red", fill="lightblue", size=10) +
scale_shape_identity() +
facet_wrap(~pt) +
theme_bw() +
theme(axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank())

Requesting solid shapes (the default) gives 16, 17, 15, 3, 7, and 8.
Requesting not solid shapes gives 1, 2, 0, 3, 7, 8. Neither set includes
the fillable ones (21-25), so you have to specify them using a manual
scale as you do in your second example.

The problem with the legend in the second example is that the shape of
the points in the legend is, by default, 16. But fill does not have an
effect of shape 16, so the legend is all black circles. Shape 21 is a
better choice and that can be specified with

ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(aes(shape=factor(vs), fill=factor(am)), size=4) +
scale_shape_manual(values=c(21, 23)) +
scale_fill_discrete(guide = guide_legend(override.aes = aes(shape = 21)))


--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University

Mark

unread,
Nov 6, 2012, 12:32:11 PM11/6/12
to ggplot2
Thank you Brian. That helped a lot.
Reply all
Reply to author
Forward
0 new messages