Changing background color for a text annotation to increase contrast and visibility

Andy Stein picture Andy Stein · Sep 20, 2016 · Viewed 13.5k times · Source

I'd like to change the background color for my annotate text so that it's green and covers up anything behind it (like the horizontal line in the example below). How do I do that?

ggplot() + 
  geom_hline(yintercept=0) + 
  annotate("text",x=0,y=0,label="Here is a line")

enter image description here

Answer

Martin Schmelzer picture Martin Schmelzer · Sep 20, 2016

Try geom_label instead:

ggplot() + 
  geom_hline(yintercept = 0) + 
  labs(x = "", y = "") +
  geom_label(aes(x = 0, y = 0, label = "Here is a line"), fill = "green")

enter image description here