Changing background color for a text annotation to increase contrast and visibility
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")
Building on this answer, but avoiding the use of geom_label()
so that the label draws only once, not once for every row of plotted data (as correctly pointed out in this comment):
You can still use annotate()
, which is the preferred approach for a one-off annotation, but use label
instead of text
as the geom
.
Likewise you could supply geom="segment"
to draw a line, etc...
ggplot() +
geom_hline(yintercept=0) +
annotate(geom="label",x=0,y=0,label="Here is a line", fill="green")