riatelab / mapsf

Thematic cartography with R
https://riatelab.github.io/mapsf
GNU General Public License v3.0
224 stars 23 forks source link

pdf export with dev.copy() #75

Closed mgageo closed 4 days ago

mgageo commented 2 weeks ago

Je sauvegarde en pdf ou en png la fenêtre graphique avec dev.copy. Alors qu'à l'écran le titre s'affiche complètement, le caractère de droite n'est pas visible sur les copies. à l'écran image en png, perte du "1" final image

Le script : https://github.com/mgageo/issues/blob/main/issue_mapsf4.R

library(sf)
library(mapsf)
library(tidyverse)
dsn <- "https://raw.githubusercontent.com/mgageo/issues/ee547796e3a99c966c2d0890f4807bf40f14f598/shape_stops_POSB-POSD-alsac-LYH.geojson"
nc1 <- st_read(dsn) %>%
  st_transform(2154) %>%
  glimpse()
shape.sf <- nc1 %>%
  filter(st_geometry_type(geometry) != "POINT") %>%
  glimpse()
points.sf <- st_sf(st_cast(st_geometry(shape.sf), "POINT"))

mf_init(shape.sf, expandBB = c(0.3, 0.3, 0.3, 0.3))
mf_map(x = shape.sf, col = "green", lwd = 3, add = TRUE)
mf_annotation(points.sf[1, ], txt = "Départ", col_txt = "blue", col_arrow = "blue")
mf_title("r1268247 : 0061-A-1510-3801", inner = TRUE)
w <- par("din")[1]
h <- par("din")[2]
dir <- tempdir()
dsn <- sprintf("%s/issue_mapsf4.pdf", dir)
print(sprintf("dsn: %s", dsn))
dev.copy(pdf, dsn, width = w, height = h)
dev.off()
dsn <- sprintf("%s/issue_mapsf4.png", dir)
print(sprintf("dsn: %s", dsn))
dev.copy(png, dsn)
dev.off()
rCarto commented 1 week ago

The PDF export does not use exactly the same font as the R graphic device (you can see that with the "-"). It can probably be set with some options but it is not specific to mapsf. For a vector format export an SVG export is more accurate. For a PNG export you can set the width and height of the figure.

library(mapsf)
m <- mf_get_mtq()
mf_map(m)
mf_title("Test--Tiret")
# device size
w <- dev.size("in")[1]
h <- dev.size("in")[2]
# export pdf
(dsn <- paste0(tempdir(), "/mapsf_devcopy.pdf"))
dev.copy(pdf, dsn, width = w, height = h)
dev.off()
# export svg 
(dsn <- paste0(tempdir(), "/mapsf_devcopy.svg"))
dev.copy(svg, dsn, width = w, height = h)
dev.off()
# export png
(dsn <- paste0(tempdir(), "/mapsf_devcopy.png"))
dev.copy(png, dsn, width = w*72, height = h*72, type = "cairo")
dev.off()

PDF : mapsf_devcopy.pdf
SVG :
mapsf_devcopy
PNG :
mapsf_devcopy

The prefered way to export maps with mapsf is with the mf_export() function. See https://riatelab.github.io/mapsf/articles/web_only/how_to_export_maps.html

mgageo commented 4 days ago

Merci pour votre recherche de l'origine du problème, je contourne en ajoutant une espace à la fin du texte.