更改文本注释的背景颜色以增加对比度和可见性

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

我想更改注释文本的背景颜色,使其为绿色并遮盖其后面的所有内容(如下例中的水平线)。我该怎么做?

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

请尝试 geom_label

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

基于 , 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 ):

您仍然可以使用 annotate(),这是一次性注释的首选方法,但使用 label 而不是 text 作为 geom

同样,您可以提供 geom="segment" 来画一条线等...

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