Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buffer for text option #1039

Closed
Robinlovelace opened this issue Jan 11, 2015 · 6 comments
Closed

Buffer for text option #1039

Robinlovelace opened this issue Jan 11, 2015 · 6 comments

Comments

@Robinlovelace
Copy link

This is a feature request that could make labeling in complex graphics much more aesthetically pleasing: buffers - where you have an area of another colour (typically white) slightly larger than the main text behind the main text to allow the text to be read regardless of what's behind it. This is commonly used in maps, and is implemented to very good effect in QGIS: http://manual.linfiniti.com/en/vector_classification/label_tool.html

@hadley
Copy link
Member

hadley commented Jun 11, 2015

This is do-able, but fiddly because you have to figure out the size of the text at draw time - this means effectively you have to create a custom grid grob. However, I vaguely remember seeing that somewhere, so I'll see it's available.

It would also be useful to expose the check.overlap argument - it's not perfect (since you can't control what gets removed) but it might be helpful in some situations.

@Robinlovelace
Copy link
Author

Worth asking the QGIS guys how they do it I'd say. I'll look into it. Cheers.

hadley added a commit that referenced this issue Jun 15, 2015
@hadley
Copy link
Member

hadley commented Jun 15, 2015

I think the right way to tackle this is to develop a new grid grob. Here's a start from Paul Murrell:

library(grid)

textbg <- function(t, x, y, col) {
  grid.draw(gTree(label = t, x = x, y = y, col = col, cl = "textbg"))
}

#' @export
makeContent.textbg <- function(x, ...) {
  tg <- textGrob(x$label, x$x, x$y)
  bg <- roundrectGrob(x$x, x$y,
    width = grobWidth(tg) + unit(2, "mm"),
    height = grobWidth(tg) + unit(2, "mm"),
    gp = gpar(
      col = NA, 
      fill = x$col
    )
  )
  setChildren(x, gList(bg, tg))
}

grid.newpage()
textbg("test", .5, .5, "green")

I don't have the time to develop it further at the moment.

@Robinlovelace
Copy link
Author

Cool.

And I don't think I have the skills!

Will make time to read the grob section in an attempt to aquire them. Would you recommend this to go into core ggplot2 or an add-on package in the unlikely event that I can do something with this or in the likelier event that someone else can?

Will be useful for ggmap.

Related question: are there any elements of ggmap that need to adjust to accomodate ggplot2 1.0.0?

@hadley hadley closed this as completed in fb2c4e9 Jul 24, 2015
@t-kalinowski
Copy link

Any updates on this? A halo option for text labels would be useful.

@lock
Copy link

lock bot commented Sep 8, 2018

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Sep 8, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants