9
votes

Example code and figure:

data <- data.frame( ID = c(LETTERS[1:26], paste0("A",LETTERS[1:26])),
                    Group = rep(c("Control","Treatment"),26),
                    x = rnorm(52,50,20),
                    y = rnorm(52,50,10))

ggplot(data, aes(y=y,x=x, label=ID, color=Group)) + 
   geom_text(size=8) + 
   scale_color_manual(values=c("blue","red")) +
   theme_classic() +
   theme(legend.text = element_text(color=c("blue","red")))

enter image description here

What I'm trying to solve is removing the legend symbols (the "a") and coloring the Group labels (Control and Treatment) as they appear in the plot (Blue and Red respectively).

I've tried:

geom_text(show_guide = F)

But that just removes the legend entirely.

To keep it simple I could just use annotate...but wondering if there's a legend specific solution.

ggplot(data, aes(y=y,x=x, label=ID, color=Group)) +
   geom_text(size=8, show_guide=F) +
   scale_color_manual(values=c("blue","red")) +
   theme_classic() +
   annotate("text",label="Control", color="blue",x=20,y=80,size=8) +
   annotate("text",label="Treatment", color="Red",x=23,y=77,size=8)
3

3 Answers

9
votes

Another option is to use point markers (instead of the letter "a") as the legend symbols, which you can do with the following workaround:

  1. Remove the geom_text legend.
  2. Add a "dummy" point geom and set the point marker size to NA, so no points are actually plotted, but a legend will be generated.
  3. Override the size of the point markers in the legend, so that point markers will appear in the legend key to distinguish each group.

ggplot(data, aes(y=y,x=x, label=ID, color=Group)) + 
  geom_text(size=8, show.legend=FALSE) + 
  geom_point(size=NA) +
  scale_color_manual(values=c("blue","red")) +
  theme_classic() +
  labs(colour="") +
  guides(colour=guide_legend(override.aes=list(size=4)))

enter image description here

1
votes

As a quick fix you can tweak the legend key, by hard coding the info you want, although around the other way - keep the key and remove the label.

library(grid)

GeomText$draw_key <- function (data, params, size) {
    txt <- ifelse(data$colour=="blue", "Control", "Treatment") 
    # change x=0 and left justify 
    textGrob(txt, 0, 0.5,  
             just="left", 
             gp = gpar(col = alpha(data$colour, data$alpha), 
                       fontfamily = data$family, 
                       fontface = data$fontface, 
                       # also added 0.5 to reduce size
                       fontsize = data$size * .pt* 0.5))
}

And when you plot you suppress the legend labels, and make legend key a bit wider to fit text.

ggplot(data, aes(y=y,x=x, label=ID, color=Group)) + 
   geom_text(size=8) + 
   scale_color_manual(values=c("blue","red")) +
   theme_classic() +
   theme(legend.text = element_blank(),
         legend.key.width = unit(1.5, "cm"))
1
votes

Beginning with ggplot2 2.3.2, you can specify the glyph used in the legend using the argument key_glyph:

ggplot(data, aes(x=x, y=y, label=ID, color=Group)) + 
  geom_text(size=8, key_glyph="point") + 
  scale_color_manual(values=c("blue", "red")) +
  labs(color=NULL) +
  theme_classic()

ggplot showing text legend with dot glyphs instead of the letter a

For a full list of glyphs, refer to the ggplot2 documentation for draw_key. Credit to R Data Berlin for alerting me to this simple solution. Emil Hvitfeldt also has a nice blog post showcasing the options.