bg.color in tm_text in v4, is it going to be replaced?
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?
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")
tm_shape(World) +
tm_text("name", bgcol = "economy")
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".
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'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.
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:
- Save a copy of the original locations
- Shuffle the overlapping boxes around until they don't overlap
- Draw lines from the original locations to box centroids
- 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.
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).
To be continued in #850