Search code examples
rggplot2ggrepel

ggplot: How to keep marker colours in legend but hide text colours?


I am looking to change the colour of some, but not all, labels so they stand out in a populated plot. I have achieved this using the following:

df <- data.frame("x"=1:10, "y"=1:10, "dir"=rep(c("a", "b"), 5))

library(ggplot2)
library(ggrepel)
ggplot(data=df, aes(x, y, color=dir)) +
  geom_point(show.legend=TRUE) +
  geom_text_repel(data=df[1:5, ], 
                  aes(x=x, y=y, 
                      color=factor(df$dir[1:5], labels=c("text a", "text b")), 
                      label=dir), 
                  size=2.5, force=15, show.legend=FALSE) + 
  scale_colour_manual(values=c("salmon", "black", "salmon", "darkturquoise"))

How can I do this whilst hiding the label colour scheme in the legend (i.e. get rid of text a and text b in the plot below)? show.legend=FALSE doesn't seem to work and I want to keep the point legend.

enter image description here

In this example I am using geom_text_repel, but I imagine it's the same for geom_text.

Thanks!


Solution

  • You may specify the breaks manually too by the replacing the last line with

    scale_colour_manual(breaks = c("a", "b"), values = c("salmon", "black", "salmon", "darkturquoise"))
    

    enter image description here