Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove 'a' from legend when using aesthetics and geom_text

People also ask

How do you remove a legend?

Tip: To quickly remove a legend or a legend entry from a chart, you can select it, and then press DELETE. You can also right-click the legend or a legend entry, and then click Delete.

How do you remove the legend from a Boxplot?

By specifying legend. position=”none” you're telling ggplot2 to remove all legends from the plot.

How do I get rid of the legend in ggplot2?

Example 1: Remove All Legends in ggplot2 We simply had to specify legend. position = “none” within the theme options to get rid of both legends.


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


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) +
..

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)


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