Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6655985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:35:16+00:00 2026-05-26T01:35:16+00:00

I am developing a graphic with ggplot2 wherein I need to superimpose text over

  • 0

I am developing a graphic with ggplot2 wherein I need to superimpose text over other graphical elements. Depending on the color of the elements underlying the text, it can be difficult to read the text. Is there a way to draw geom_text in a bounding box with a semi-transparent background?

I can do this with plotrix:

library(plotrix)
Labels <- c("Alabama", "Alaska", "Arizona", "Arkansas")
SampleFrame <- data.frame(X = 1:10, Y = 1:10)
TextFrame <- data.frame(X = 4:7, Y = 4:7, LAB = Labels)
### plotrix ###
plot(SampleFrame, pch = 20, cex = 20)
boxed.labels(TextFrame$X, TextFrame$Y, TextFrame$LAB,
 bg = "#ffffff99", border = FALSE,
 xpad = 3/2, ypad = 3/2)

But I do not know of a way to achieve similar results with ggplot2:

### ggplot2 ###
library(ggplot2)
Plot <- ggplot(data = SampleFrame,
 aes(x = X, y = Y)) + geom_point(size = 20)
Plot <- Plot + geom_text(data = TextFrame,
 aes(x = X, y = Y, label = LAB))
print(Plot)

As you can see, the black text labels are impossible to perceive where they overlap the black geom_points in the background.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:35:17+00:00Added an answer on May 26, 2026 at 1:35 am

    Try this geom, which is slightly modified from GeomText.

    GeomText2 <- proto(GeomText, {
      objname <- "text2"
      draw <- function(., data, scales, coordinates, ..., parse = FALSE,
                       expand = 1.2, bgcol = "grey50", bgfill = NA, bgalpha = 1) {
        lab <- data$label
        if (parse) {
          lab <- parse(text = lab)
        }
    
        with(coordinates$transform(data, scales), {
          tg <- do.call("mapply",
            c(function(...) {
                tg <- with(list(...), textGrob(lab, default.units="native", rot=angle, gp=gpar(fontsize=size * .pt)))
                list(w = grobWidth(tg), h = grobHeight(tg))
              }, data))
          gList(rectGrob(x, y,
                         width = do.call(unit.c, tg["w",]) * expand,
                         height = do.call(unit.c, tg["h",]) * expand,
                         gp = gpar(col = alpha(bgcol, bgalpha), fill = alpha(bgfill, bgalpha))),
                .super$draw(., data, scales, coordinates, ..., parse))
        })
      }
    })
    
    geom_text2 <- GeomText2$build_accessor()
    
    Labels <- c("Alabama", "Alaska", "Arizona", "Arkansas")
    SampleFrame <- data.frame(X = 1:10, Y = 1:10)
    TextFrame <- data.frame(X = 4:7, Y = 4:7, LAB = Labels)
    
    Plot <- ggplot(data = SampleFrame, aes(x = X, y = Y)) + geom_point(size = 20)
    Plot <- Plot + geom_text2(data = TextFrame, aes(x = X, y = Y, label = LAB),
                              size = 5, expand = 1.5, bgcol = "green", bgfill = "skyblue", bgalpha = 0.8)
    print(Plot)
    

    BUG FIXED AND CODE IMPROVED

    GeomText2 <- proto(GeomText, {
      objname <- "text2"
      draw <- function(., data, scales, coordinates, ..., parse = FALSE,
                       expand = 1.2, bgcol = "grey50", bgfill = NA, bgalpha = 1) {
        lab <- data$label
        if (parse) {
          lab <- parse(text = lab)
        }
        with(coordinates$transform(data, scales), {
          sizes <- llply(1:nrow(data),
            function(i) with(data[i, ], {
              grobs <- textGrob(lab[i], default.units="native", rot=angle, gp=gpar(fontsize=size * .pt))
              list(w = grobWidth(grobs), h = grobHeight(grobs))
            }))
    
          gList(rectGrob(x, y,
                         width = do.call(unit.c, lapply(sizes, "[[", "w")) * expand,
                         height = do.call(unit.c, lapply(sizes, "[[", "h")) * expand,
                         gp = gpar(col = alpha(bgcol, bgalpha), fill = alpha(bgfill, bgalpha))),
                .super$draw(., data, scales, coordinates, ..., parse))
        })
      }
    })
    
    geom_text2 <- GeomText2$build_accessor()
    

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For a Website I'm developing, I will need to create some graphics. I haven't
Developing a .NET WinForms application: how can I check if the window is in
While there are many resources for Icons, free graphic files why can't i find
I'm developing an OpenGL application. I need to have a model of planet earth
At present we are developing a graphic app on a linux box using opengl.
Hi I am developing an ASP.net web application. I need to find the current
I'm a newbie developing my second project in Ruby on Rails: A graphic calculator.
I'm developing a REST API, and am looking for a simple graphic utility that
I'm developing a WindowsForms application and I need to highlight some figures defined with
I am developing a report that incorporates a graphical header on only the first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.