这是indexloc提供的服务,不要输入任何密码
Skip to content

Allowing for other graphic devices #585

@Nowosad

Description

@Nowosad

The tmap_save() function guesses the expected graphic devices from the file extension. This is great in most cases, however, it does not allow to use any other graphic devices except the few that are build-in. This is in contrast to the ggsave(), which has the device argument - https://ggplot2.tidyverse.org/reference/ggsave.html.

Why does anyone want to use any other graphic devices? (1) they could render things nicer than some default graphic devices, (2) they could allow using foreign characters or external fonts, (3) ragg is not the default ggplot2 graphic device for raster graphical formats, however, it cannot be used with tmap_save(). See a small motivational example below.

@mtennekes, would it be possible to add the device argument to tmap_save()?

library(tmap)
#> Registered S3 methods overwritten by 'stars':
#>   method            from
#>   st_crs.SpatRaster sf  
#>   st_crs.SpatVector sf
library(spData)

# adds foreign characters
nz$Name2 = paste0(nz$Name, "śżć")

# creates a map
tm = tm_shape(nz) +
  tm_text(text = "Name2", fontfamily = "sans", fontface = "italic")
tm

# saves using tmap_save
tmap_save(tm, "tm.pdf")
#> Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
#> conversion failure on 'Northlandśżć' in 'mbcsToSbcs': dot substituted for <c5>
#> Map saved to /tmp/RtmpWIcJli/reprex-65256290a1b4-wax-mouse/tm.pdf
#> Size: 5.819444 by 8.402778 inches

tm.pdf

# saves using external device
cairo_pdf("tm2.pdf")
tm
dev.off()
#> png 
#>   2

tm2.pdf

Created on 2021-08-11 by the reprex package (v2.0.1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions