I was wondering if there is a simple way to add text labels with a contrasting background to an R plot using the base graphic system. Until now I have always used the rect
Base graphics
Using legend
:
plot(x = runif(1000), y = runif(1000), type = "p", pch = 16, col = "#40404050")
legend(0.4, 0.5, "Some text", box.col = "lightblue", bg = "lightblue", adj = 0.2)
Output:
ggplot2
With geom_label
:
library(ggplot2)
df <- data.frame(x = runif(1000), y = runif(1000))
ggplot(data = df, aes(x = x , y = y))+
geom_point(alpha = 0.2)+
geom_label(aes(x = 0.5, y = 0.5, label = "Some text"),
fill = "lightblue", label.size = NA, size = 5)
Output: