Remove 'a' from legend when using aesthetics and geom_text
Asked Answered
O

8

199

How can I can remove the letter 'a' from the legend generated by this code? If I remove the geom_text, then the 'a' letter will not show in the legend. I want to keep geom_text, though.

ggplot(data = iris, aes(x = Sepal.Length, y=Sepal.Width, 
                        shape = Species, colour = Species)) + 
   geom_point() + 
   geom_text(aes(label = Species))
Offoffbroadway answered 20/8, 2013 at 14:29 Comment(0)
T
248

Set show.legend = FALSE in geom_text:

ggplot(data = iris,
       aes(x = Sepal.Length, y = Sepal.Width, colour = Species,
           shape = Species, label = Species)) + 
    geom_point() +
    geom_text(show.legend = FALSE)

The argument show_guide changed name to show.legend in ggplot2 2.0.0 (see release news).


Pre-ggplot2 2.0.0:

With show_guide = FALSE like so...

ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width , colour = Species,
                        shape = Species, label = Species ), size = 20) + 
geom_point() +
geom_text(show_guide  = FALSE)

enter image description here

Tricostate answered 20/8, 2013 at 14:46 Comment(4)
Setting show.legend to FALSE in ggplot2 3.2.1 will remove the legend altogether!Fourflush
(a) in ggplot2 3.3.5 show.legend=FALSE preserves the overall legend and (b) I am surprised at how difficult finding this wasGodmother
This does not seem to work when the aes() part is included inside geom.txt and returns "Ignoring unknown aesthetics: show.legend"Chrystalchryste
@Chrystalchryste that sounds like you put show.legend = FALSE inside aes(). It should be inside geom_text(), but not inside aes(). So geom_text(aes(label = Species), show.legend = FALSE) is good, geom_text(aes(label = Species, show.legend = FALSE)) is bad.Chavez
L
35

We can use guide_legend(override.aes = aes(...)) to hide the 'a' in the legend.

Below is a short example of how you might use guide_legend()

library(ggrepel)
#> Loading required package: ggplot2

d <- mtcars[c(1:8),]

p <- ggplot(d, aes(wt, mpg)) +
  geom_point() +
  theme_classic(base_size = 18) +
  geom_label_repel(
    aes(label = rownames(d), fill = factor(cyl)),
    size = 5, color = "white"
  )

# Let's see what the default legend looks like.
p

# Now let's override some of the aesthetics:
p + guides(
  fill = guide_legend(
    title = "Legend Title",
    override.aes = aes(label = "")
  )
)

Created on 2019-04-29 by the reprex package (v0.2.1)

Langsyne answered 29/4, 2019 at 19:29 Comment(2)
I think this is a better solution than the accepted one, as it allows to specifically remove the 'a' letter from the legend, while other aesthetics can, if desired, remain untouched.Talus
In case that color is used instead of fill it could be guides( color = guide_legend(override.aes = aes(label = "")))Pedology
L
17

I had a similar problem. Simon's solution worked for me but a slight twist was required. I did not realise that I need to add "show_guide = F" to geom_text's arguments, rather than replace with it the existing arguments - which is what Simon's solution shows. For a ggplot2 noob like me this was not that obvious. A proper example would have used the OP's code and just added the missing argument like this:

..
geom_text(aes(label=Species), show_guide = F) +
..
Ladonnalady answered 10/2, 2015 at 22:31 Comment(1)
This worked for me, but show_guide is now deprecated. Use show.legend instead.Phytohormone
P
8

Like Nick said

the following code would still produce the error:

geom_text(aes(x=1,y=2,label="",show_guide=F))

enter image description here

whereas:

geom_text(aes(x=1,y=2,label=""),show_guide=F)

outside the aes argument eliminates the a over the legend

enter image description here

Proulx answered 15/8, 2015 at 14:14 Comment(1)
Is there any way to customize the 'a' to something else like 'r'?Pinette
O
5

I had a similar problem, with an 'a' appearing behind different coloured points I was trying to label with geom_text_repel. To remove the 'a', so that it would just show the point without the 'a' behind it, I had to add show.legend=FALSE as an argument in geom_text_repel.

Hope that makes sense to anyone who might be labouring with the same issue!

Outmoded answered 23/5, 2020 at 16:39 Comment(0)
H
3

You can also use show.legend = FALSE in the arguments of geom_label_repel() to remove the "a" in the legend. So, instead of

ggplot(d, aes(wt, mpg)) +
  geom_point() +
  theme_classic(base_size = 18) +
  geom_label_repel(
    aes(label = rownames(d), fill = factor(cyl)),
    size = 5, color = "white"
  )+ guides(
  fill = guide_legend(
    title = "Legend Title",
    override.aes = aes(label = "")
  )
)

you can do,

ggplot(d, aes(wt, mpg)) +
  geom_point() +
  theme_classic(base_size = 18) +
  geom_label_repel(
    aes(label = rownames(d), fill = factor(cyl)),
    size = 5, color = "white",
    show.legend = FALSE  )
Humfrey answered 17/3, 2020 at 21:7 Comment(0)
R
2

Building off the top answer. If you only wanted the geom_text() to be visible but have that geom_point() for the purposes of your legend you could set the alpha to 0 making it invisible but override it to 1 in the guides to force it to show.

ggplot(data = iris, aes(x = Sepal.Length, y=Sepal.Width, 
                        shape = Species, colour = Species)) + 
  geom_point(alpha = 0) + 
  geom_text(aes(label = Species)) +
  guides(color = guide_legend(override.aes = aes(label = "", alpha = 1))) 

enter image description here

Rickettsia answered 7/4, 2023 at 20:8 Comment(0)
D
0

In case it helps, even I'm nob.

stat_cor() also needs show.legend=FALSE to avoid "a".

Wasted time for me... ;)

Many thanks for all your contributions

Distortion answered 3/2, 2024 at 18:22 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.