使用terra::north(type=2)导出PDF时出现符号替换警告求助
问题:
terra::north(type=2)添加指北针导出PDF时字符替换警告及显示异常 使用terra::north(type = 2)为绘图添加指北针,并用pdf()导出PDF时会触发以下警告:
In text.default(x, y, symbol, cex = cex * mcex, srt = rangle, xpd = xpd, :
for '➢' in 'mbcsToSbcs': -> substituted for ➢ (U+27A2)
生成的PDF中箭头符号会被替换为其他字符。测试发现type=1可正常渲染,type=2到type=12均会触发该警告。
测试代码
library(terra) f <- system.file("ex/elev.tif", package="terra") r <- terra::rast(f) pdf("test.pdf") plot(r) terra::north("topleft", type = 2) # type 2 produces the warning dev.off()
环境信息
- R版本:4.5.0
terra版本:1.8.60- 操作系统:Windows 11
解决方法
1. 改用Cairo PDF设备
Windows默认的pdf()设备对Unicode特殊字符支持有限,换成cairo_pdf()可以完美渲染这些箭头符号:
library(terra) f <- system.file("ex/elev.tif", package="terra") r <- terra::rast(f) cairo_pdf("test_cairo.pdf") plot(r) terra::north("topleft", type = 2) dev.off()
2. 自定义指北针符号
如果不想更换PDF设备,可手动替换为系统支持的简单箭头符号,比如↑:
library(terra) f <- system.file("ex/elev.tif", package="terra") r <- terra::rast(f) pdf("test_custom.pdf") plot(r) north("topleft", symbol = "↑", cex=1.5) # 自定义符号和大小 dev.off()
3. 指定支持Unicode的字体
调用pdf()时指定支持U+27A2字符的字体(如微软雅黑、Noto Sans等):
library(terra) f <- system.file("ex/elev.tif", package="terra") r <- terra::rast(f) pdf("test_font.pdf", family="Microsoft YaHei") plot(r) terra::north("topleft", type = 2) dev.off()
内容的提问来源于stack exchange,提问作者lsalinasm23
相关产品推荐
相关产品推荐

