tmap icon indicating copy to clipboard operation
tmap copied to clipboard

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

Open staropram opened this issue 2 years ago • 5 comments

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?

staropram avatar Nov 06 '23 15:11 staropram

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".

mtennekes avatar Nov 10 '23 18:11 mtennekes

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?

staropram avatar Nov 13 '23 08:11 staropram

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.

mtennekes avatar Nov 29 '23 18:11 mtennekes

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.

staropram avatar Jan 03 '24 14:01 staropram

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 avatar Feb 02 '24 08:02 mtennekes

To be continued in #850

mtennekes avatar Mar 25 '24 12:03 mtennekes