Description
Occasionally it can be useful to use filled shapes (i.e. in the range 21:25
) to separately control the colour and fill aesthetics in geom_point()
. Here's a contrived example using shape 21 that works:
library(ggplot2)
#>
#> Attaching package: 'ggplot2'
#> The following object is masked from 'package:base':
#>
#> is.element
ggplot(head(diamonds, n = 20)) +
geom_point(aes(carat, price, fill = cut), shape = 21, size = 5)
Now if we also map the shape aesthetic, the fill guide draws the keys using its default shape of 19 which doesn't support fill leading to the colours disappearing from the legend:
ggplot(head(diamonds, n = 20)) +
geom_point(aes(carat, price, fill = cut, shape = color), size = 5) +
scale_shape_manual(values = 21:25)
It can of course be fixed by overriding the shape to be 21 as in the first example:
ggplot(head(diamonds, n = 20)) +
geom_point(aes(carat, price, fill = cut, shape = color), size = 5) +
scale_fill_ordinal(guide = guide_legend(override.aes = list(shape = 21))) +
scale_shape_manual(values = 21:25)
Created on 2024-11-06 with reprex v2.1.1
This isn't really a bug because it's behaving as intended, but it's still a bit surprising! Some possible solutions:
- Warn when constructing/drawing a guide that has
key_glyph = "point"
, a non-empty/non-constant fill, and an unfilled shape. - Note the behaviour somewhere in documentation.
- Do nothing.
Note that even though draw_key_point()
fills in a default of 19 that doesn't seem relevant in practice because the key is always draw with a non-NULL
value determined by the theme's pointshape
if shape isn't mapped for a guide.
Aside: using a key glyph outside shape scale
Even putting aside filled shapes, if my shape scale is only triangles and squares, should any of the guides really be using circles for the keys? I suspect that by now people are used to thinking of circles as the "neutral" point shape in ggplot.library(ggplot2)
#>
#> Attaching package: 'ggplot2'
#> The following object is masked from 'package:base':
#>
#> is.element
ggplot(head(diamonds, n = 20)) +
geom_point(aes(carat, price, shape = cut, colour = color), size = 5) +
scale_shape_manual(values = 2:6)
Created on 2024-11-06 with reprex v2.1.1