r-tmap / tmap

R package for thematic maps
https://r-tmap.github.io/tmap
GNU General Public License v3.0
858 stars 120 forks source link

tm_credits - Use of bquote() or expression() for subscripts / superscripts #584

Closed Michael-Aberle closed 3 years ago

Michael-Aberle commented 3 years ago

Greetings!

I have a large dataset with 60+ elements (geochemistry), for which I am using tmap to produce spatial geochemical maps. Some elements require subscripts to be printed in the following spots:

I am currently unable to use unicode for adding subscripts with the current machine, and have used expressions elsewhere in tm_layout() and tm_raster() to achieve the desired style. The current problem is with tm_credits().

The minimal working example below produces the expected style:

data("World")

tmap.compass.LHS.var <- tm_compass(north = 0, type = "4star", show.labels = 2, position = c("left","top"), size = 3, just = c("right", "top"))

tmap.scalebar.LHS <- tm_scale_bar(width = 0.25, position = c("left","top"), bg.color="white", bg.alpha = 0) +
  tm_credits("UNIVERSAL TRANSVERSE MERCATOR    PROJECTION\nGeocentric Datum of Australia 1994\n    ", position = c("left","top"), align = "left", just = "center",size = 0.5, bg.color="white", bg.alpha = 0)

tmap.maincredits <- tm_credits("Project Title" , position = c("center","bottom"), align = "center", just = "center", size = 2, col = "black", fontface = "bold.italic", bg.color="white", bg.alpha = 0) +
  tm_credits("Project Sub-title\nProject Institutions" , position = c("center","bottom"), align = "center", just = "center", size = 2, col = "black", fontface = "bold.italic", bg.color="white", bg.alpha = 0)

# This plots with correct styling
tm_shape(World) +
  tm_polygons("HPI") +
  tmap.scalebar.LHS +
  tmap.compass.LHS.var +
  tmap.maincredits

image

However, when I use a combination of bquote, list, and do.call, the correct subscripts are added, however the text formatting elsewhere in the tmap is removed.

sub_title1 <- "LiBO"[2]~"fusion, WDXRF"
text_list  <- list(bquote(.(sub_title1)~" LiBO"[2]))

tmap_subheadings <- tm_credits(text = do.call(expression, text_list), position = c("right","top"), fontface = "bold.italic", size = 2)

tm_shape(World) +
  tm_polygons("HPI") +
  tmap_subheadings +
  tmap.scalebar.LHS +
  tmap.compass.LHS.var +
  tmap.maincredits 

image

So far this is the closest I've come to correctly formatting the subscripts with pasted text (e.g. "LiBO"[2]), and text from variables ( .(sub_title1) ). However, it strips/removes the existing formatting and styles throughout the map. Again, due to the large dataset with varying subscript positions and machines, unicode is not possible.

It would be of benefit to allow expressions, or suggest a work-around.

Many thanks in advance, and your package is greatly appreciated! Thanks!

mtennekes commented 3 years ago

The problem occurred when some credits use expressions and others characters in one plot call. Should be fixed now. If not, please reopen.

Michael-Aberle commented 3 years ago

Confirmed as fixed, thank you.