3

I'm trying to create and save a map with transparent background instead of a white (or any other color) one using the R package tmap.

Apart from what's in the example below, I tried using bg.color = "transparent", bg.color = NA, and bg.color = NULL in both tmap_options and tm_layout.

Version info:

  • tmap: 2.2
  • tmaptools: 2.0-1
library(tmap)
data("World")

tmap_options (bg.color = "#00000000", basemaps.alpha = 0)
map <- tm_shape(World) +
  tm_polygons("HPI") +
  tm_layout (frame = FALSE, bg.color = "#00000000")

tmap_save (map, filename = "~/test.png")

Am I doing something wrong or is this simply a limitation of the package? Thanks a lot!

adibender
  • 7,288
  • 3
  • 37
  • 41
karpfen
  • 489
  • 8
  • 19
  • Use the `alpha` argument within the layer of interest, e.g. `tm_polygons("HPI", alpha = .5)` – adibender May 08 '19 at 08:45
  • 1
    But that makes the polygon layer transparent, not the background one, If I understand correctly. I need *only* the background layer to be transparent. – karpfen May 08 '19 at 08:49
  • Well, depends on what you want to do, but what is the background in this case, if you only have one layer? – adibender May 08 '19 at 08:51
  • That's the thing; there is no background. I want only polygons (+ legend, title and so on) on a 100% transparent background. In the example above, I want everything that's white in the resulting image to be gone. – karpfen May 08 '19 at 08:58
  • if you use `bg.color = rgb(0,0,0, alpha = 0)`? – adibender May 08 '19 at 09:13
  • Nope, also gives me a white background. – karpfen May 08 '19 at 09:16
  • There are 2 map modes in tmap - plot mode and view mode. Only the view mode seems to show background imagery like "open street map" etc. The plot mode seems to default to a white background. The examples at the link below clearly show the difference between the two modes. https://www.rdocumentation.org/packages/tmap/versions/2.2/topics/tmap_mode. Could your issue be that you are expecting a format more like the "view" mode but that you are operating in "plot" mode? – olorcain May 08 '19 at 09:21
  • Thank you, but that doesn't solve my problem, unfortunately. I need the map to be static, in PNG format, so plot mode makes much more sense to me. I only need to replace the default white background with a transparent one somehow. But for the record: I also tried using `tmap_save` in view mode, the resulting PNG file had a white, opaque background. – karpfen May 08 '19 at 09:36

2 Answers2

3

I found a trick!

I was looking for the same feature to draw a stack of maps in InDesign with outputs from R, tmap and I needed the background to be transparent.

Here my solution, given your code:

library(tmap)
data("World")

par(bg=NA)
map <- tm_shape(World) +
  tm_polygons("HPI") +
  tm_layout (frame = FALSE, bg.color = "transparent")

tmap_save (map, filename = "~/test.eps", bg="transparent") # Note the eps extension

Caveat: testing this with different output formats, I realized that it doesn't work with png or jpg.

edith
  • 46
  • 4
1

You have to specify the png function as the device argument. The png documentation says

png supports transparent backgrounds: use bg = "transparent". (Not all PNG viewers render files with transparency correctly.) When transparency is in use in the type = "Xlib" variant a very light grey is used as the background and so appears as transparent if used in the plot. This allows opaque white to be used, as in the example. The type = "cairo", type = "cairo-png" and type = "quartz" variants allow semi-transparent colours, including on a transparent or semi-transparent background.

So you just have to change your tmap_save call like this (presuming you have cairo support compiled in).

tmap_save (map, filename = "~/test.png",
           device = png, bg = "transparent", type = 'cairo')
  • For me, it only works if I specify `type = "cairo"`. – Markus Bauer Feb 09 '23 at 14:22
  • @MarkusBauer strange, the default should be "cairo" (or "quartz" on mac I guess) if "cairo" is available. From the same link as before: `type: character string, one of "Xlib" or "quartz" (some macOS builds) or "cairo". The latter will only be available if the system was compiled with support for cairo – otherwise "Xlib" will be used. The default is set by getOption("bitmapType") – the ‘out of the box’ default is "quartz" or "cairo" where available, otherwise "Xlib".` What does `getOption("bitmapType")` returns for you? It's "cairo" as expected for me (on Fedora 37) – Cedric Rossi Feb 12 '23 at 11:13
  • 1
    @MarkusBauer anyway it's probably better not to rely on compile time options anyway, so it's better to be specific! I've edited the answer accordingly. Thanks! – Cedric Rossi Feb 12 '23 at 11:28
  • It returns `NULL` – Markus Bauer Feb 13 '23 at 12:57