ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

Example of changing legend glyph for geom_text

Open ewallace opened this issue 2 years ago • 4 comments

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 = "●")))

ewallace avatar Jun 14 '22 16:06 ewallace

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)

clauswilke avatar Jun 14 '22 18:06 clauswilke

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.

ewallace avatar Jun 15 '22 09:06 ewallace

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.

clauswilke avatar Jun 15 '22 15:06 clauswilke

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.

ewallace avatar Sep 02 '22 23:09 ewallace