Text labels with background colour in R

后端 未结 4 1454
慢半拍i
慢半拍i 2021-02-20 18:19

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

4条回答
  •  逝去的感伤
    2021-02-20 18:43

    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:

提交回复
热议问题