1

I have a dataset like this:

a <- letters[1:10]
b <- c(4.04,5.12,9.69,8.77,8.39,5.77,5.62,1.85,4.58,5.00)
c <- c(14.05,5.71,11.11,9.77,9.06,7.33,6.24,2.28,7.33,5.17)
d <- c("e","f","c","d","a","e","e","b","f","b")
df <-data.frame(a,b,c,d)

I made this plot:

ggplot(data = df, aes(x= b, y= c, shape=d, color = d)) +
geom_point() + 
geom_text(aes(label=a), size=3.2, hjust = 0.5, vjust = 1.5)

And I got this:

enter image description here

How can I remove the letter "a" behind each shape in the legend?

De Novo
  • 7,120
  • 1
  • 23
  • 39
Israel
  • 260
  • 3
  • 15

1 Answers1

2

The problem here is that the geom_text layer is adding to the legend. Here are some examples to show you what is happening:

If you just call the ggplot with the geom_point layer, you get the correct legend:

p <- ggplot(data = df, aes(x = b, y = c, shape = d, color = d))
p + geom_point()

enter image description here

Try calling it with just the geom_text layer and you'll see the legend uses 'a'

p + geom_text(aes(label = a), size = 3.2, hjust = 0.5, vjust = 1.5)

enter image description here

Pass show.legend = FALSE to prevent the geom_text layer from adding to the legend:

p + geom_point() +
  geom_text(aes(label = a), size = 3.2, hjust = 0.5, vjust = 1.5, show.legend = FALSE)

enter image description here

De Novo
  • 7,120
  • 1
  • 23
  • 39