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

bg.color in tm_text in v4, is it going to be replaced? #808

Closed
staropram opened this issue Nov 6, 2023 · 6 comments
Closed

bg.color in tm_text in v4, is it going to be replaced? #808

staropram opened this issue Nov 6, 2023 · 6 comments
Labels

Comments

@staropram
Copy link

staropram commented Nov 6, 2023

Hi there, what happened to the bg.color parameter in V4 for say tm_text? It says this is now deprecated. How does one put a background color against text now?

@mtennekes
Copy link
Member

It will be a visual variable in v4. I just made a first implementation. I renamed the argument bgcol, and there will also be bgcol_alpha.

tm_shape(World) +
	tm_text("name", bgcol = "green")

image

tm_shape(World) +
	tm_text("name", bgcol = "economy")

image

Somehow, the boxes are a tad too wide, and as you can see the legend items have to be implemented accordingly.

The implementation is in branch "bgcol".

@staropram
Copy link
Author

That's awesome, great to see progress. Might I suggest while you are doing it to add in a feature like ggrepel whereby boxes don't overlap?

@mtennekes
Copy link
Member

That's awesome, great to see progress. Might I suggest while you are doing it to add in a feature like ggrepel whereby boxes don't overlap?

That would be great, but very hard to implement. Not sure ggrepel can be used for tmap, have to check. There is a similar function in tmap3, but much worse, so not sure whether to migrate or write a new implementation (or reuse ggrepel).

Any help appreciated.

@staropram
Copy link
Author

staropram commented Jan 3, 2024

Can I check, this is where you draw all the rectangles (?):

grobTextBGList = mapply(function(x, y, w, h, b, a) {
				rectGrob(x=x, y=y, width=w, height=h, gp=gpar(fill=b, alpha = a, col=NA))
			}, tGX, tGY, tGW, tGH, bgcol, bgcol_alpha, SIMPLIFY = FALSE, USE.NAMES = FALSE)

So I suppose what could be done prior to this call is:

  1. Save a copy of the original locations
  2. Shuffle the overlapping boxes around until they don't overlap
  3. Draw lines from the original locations to box centroids
  4. Draw the boxes and text

It looks like ggrepel uses an iterated force algorithm and they call it repel_boxes2 this is written in C++ and interfaced using Rpp so it should be fairly straightforward to convert the tmap boxes into a format compatible with the algorithm and back. Based on looking at it for 2 minutes that is...

I guess this would be the most direct way to do it and to reuse what has already been done.

I've only briefly skimmed through your code but I guess it would make sense that tm_text is reused and an option added to tm_text like repel=TRUE ? Otherwise it would require a lot of duplication if a tm_text_repel was created?

I really want to try this out but I know I don't have time at the minute. I'm in such a debt to all the open-source developers out there.

@mtennekes
Copy link
Member

Merged the bgcol branch into master.

@staropram About the repel feature:

  • Yes, it makes sense to add it to tm_text
  • Your algorithm seems a good starting point and fairly easy to implement. The randomisation enables users to rerun the repel until they like the outcome.
  • Ultimately, I'd like to use that iterated force algorithm for tmap, at least that seems more sophisticated. Do you know if they also take weights into account? (E.g. names of large countries should be repelled less than names of small countries).

@mtennekes
Copy link
Member

To be continued in #850

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants