ggplot2
ggplot2 copied to clipboard
Example of changing legend glyph for geom_text
A common use-case is to want to change the legend glyph for geom_text from
E.G. This stack overflow question has 48k views: https://stackoverflow.com/questions/18337653/remove-a-from-legend-when-using-aesthetics-and-geom-text
This pull request adds to geom_text
help a simple example of replacing the a with a dot / unicode black circle, using guide_legend
.
p <- ggplot(mtcars, aes(wt, mpg, label = rownames(mtcars)))
# ... etc
# Customise legend, change legend label from a to a dot
p + geom_text(aes(colour = factor(cyl))) +
scale_colour_hue("Cylinders",
guide = guide_legend(override.aes = aes(label = "●")))
Isn't this simpler?
library(tidyverse)
ggplot(mtcars, aes(wt, mpg, label = rownames(mtcars))) +
geom_text(aes(colour = factor(cyl)), key_glyph = "point")
Created on 2022-06-14 by the reprex package (v2.0.0)
Yes that is simpler and a very good argument for including an example in the geom_text
help page.
Stack overflow etc haven't caught up with the nice new feature of key_glyph
so I think examples in help will do a lot to make it discoverable.
Currently (ggplot2 3.3.5) I only found key_glyph
referred to in help for:
Would it help to add examples to any other geom_
help pages directly?
I'm happy to update the pr after this advice.
I think adding one example each to geom_text()
and geom_line()
would be good. Those are probably the geoms where people most likely want to change the key glyph. For geom_line()
I'd demonstrate the timeseries glyph.
If you can integrate it into one of the existing examples, so we don't increase the computational requirement to run all the examples, that's even better. I just looked quickly at geom_text()
. It has tons of examples, including some with legends. One of two of those can be repurposed I think.
I've now integrated key_glyph into 1 example each of geom_text and geom_line. Hopefully that's good to merge when tests pass?
Thanks for the help and sorry for the delay.