如何修复 Linux/Fedora 31 上 R 中度数符号未正确显示的问题

How to fix degree symbol not showing correctly in R on Linux/Fedora 31

我制作的任何地图:

ggplot() + geom_sf()

生成了预期的地图,但没有正确显示度数符号,如下图所示。

此答案中给出的答案 - - 没有帮助,我发布了一个单独的问题,因为我看到了不同的标志。

更改字体没有帮助。

我也试过在 renv 的单独项目中从 Github 安装当前版本的 sf (0.9),但结果相同。

我正在使用 Linux,Fedora 31。

要复制:

library("ggplot2")
library("sf")
nc <- st_read(system.file("shape/nc.shp", package="sf"))

ggplot() +
  geom_sf(data = nc)

2019-03-15更新

该问题并非特定于 ggplot/geom_sf;正如评论中所建议的那样,我遇到了与 plot.new(); text(0.5,0.5, bquote(120*degree*N), cex=5) 相同的问题

此外,澄清一下,我没有在 ?X11() 的帮助中详细说明与 Wine 相关的问题。如果我在我的终端 运行 fc-match Symbol,我得到:

StandardSymbolsPS.t1: "Standard Symbols PS" "Regular"

更新2019-03-19

已确认全新 Fedora 31 和 Fedora 32 测试版安装。可能是 Fedora 的问题。

我尝试了不同的语言环境(包括 "en_US.UTF-8" 或 "German")和设备(例如 cairo_pdf()cairo_ps()),结果相同。

X11.options() 将 "cairo" 显示为类型(将其更改为 Xlib,或 dbcairo 不会更改结果)。

使用 dww 在下面的答案中提出的 TestChars() 功能显示如下:

但是,如果我用 knitr 编织成 pdf,我会得到大多数符号,包括 °。

如果我编织到 html,我会得到通常的乱码。

2020-03-20更新

正如@jpmam1 所建议的,这似乎与 pango 中的回归有关,可以通过降级 pango 暂时修复。然而,降级 pango 会破坏 OS 的其他核心部分,例如 nautilus.

我在 Fedora 上打开了一个错误:https://bugzilla.redhat.com/show_bug.cgi?id=1815128

以下是在 Fedora 31 VirtualBox VM 上使用 R (ver3.6.3 2020-02-29) / R Studio (ver1.2.5033) 运行 解决问题的两个解决方案:

1) 使用自定义比例插入 unicode 字符以生成正确的符号(包括 N/S 或 E/W,具体取决于您的 long/lat):

#install.packages("ggplot2")
#install.packages("sf")
library("ggplot2")
library("sf")

nc <- st_read(system.file("shape/nc.shp", package="sf"))

ggplot() +
  geom_sf(data = nc) +
  scale_x_continuous(labels = function(x) paste0(x, '\u00B0', "W")) +
  scale_y_continuous(labels = function(x) paste0(x, '\u00B0', "N"))

2) 降级pango库。在 Fedora 31 中,pango 升级到 1.44,由于从 Freetype 切换到 HarfBuzz,它会影响位图字体,例如默认的 R-Studio 字体。降级包修复了系统范围内特殊字符的渲染。这应该也能解决 Fedora 32 中的问题(未经测试)。

sudo dnf downgrade --releasever 30 pango-1.43.0-4.fc30.x86_64

这与其说是一个答案,不如说是一些可以尝试的诊断,这些诊断太长了,无法发表评论。

请注意,您看到的 "strange symbol" 是当符号在指定字体中不可用时所得到的。

在我们陷入诊断之前,还要注意来自 ?plotmath:

On Unix-alikes: In a UTF-8 locale any Unicode character can be entered, perhaps as a \uxxxx or \Uxxxxxxxx escape sequence, but the issue is whether the graphics device is able to display the character. The widest range of characters is likely to be available in the X11 device using cairo: see its help page for how installing additional fonts can help. This can often be used to display Greek letters in bold or italic.

In non-UTF-8 locales there is normally no support for symbols not in the languages for which the current encoding was intended.

现在尝试调查原因的一些事情:

1。获取有关您所在地区的信息

Sys.getlocale()

2。要查看您的默认 x11 设置:

X11.options()

我们对 type 特别感兴趣,看看 x11 设备是否正在使用 cairo。如果不是,请尝试在 X11.options() 中设置 cairo 选项以查看是否有帮助

3。要查看可用的字符,请输入以下内容:

TestChars <- function(...)
{
  info = l10n_info()
  r <- c(32:126, 160:254)
  par(pty = "s")
  plot(c(-1,10), c(20,260), type = "n", xlab = "", ylab = "", xaxs = "i", yaxs = "i")
  grid(11, 24, lty = 1)
  mtext(paste("MBCS:", info$MBCS, "  UTF8:", info$`UTF-8`, "  Latin:", info$`Latin-1`))
  for(i in r) try(points(i%%10, 10*i%/%10, pch = i, font = 5,...))
  points(6,170, col='red', cex=5)
}
TestChars()

在我的系统上它看起来像这样(注意我画了一个圆圈来突出显示符号 176,您遇到问题的那个)。

4。查看其他图形设备上可用的符号

尝试在不同的设备上使用 TestChars 函数,看看是否有完整的设备。例如,

cairo_pdf()
TestChars()
dev.off()

如果在尝试这些之后您仍然卡住了,请将诊断结果输入到您的问题中,以防他们可以帮助其他人解决问题。

事实证明,这种不当行为是由 R 本身对符号的遗留使用引起的。

这可能会在 R 本身的上游得到修复: https://bugs.r-project.org/bugzilla/show_bug.cgi?id=17748 在收到对此问题的反馈后,它引用了我为 Fedora 打开的下游问题: https://bugzilla.redhat.com/show_bug.cgi?id=1815128

Iñaki Ucar 在 R 邮件列表上发布了一个有效的解决方法: https://stat.ethz.ch/pipermail/r-devel/2020-March/079185.html

复制过来供参考:

$ sudo dnf install gdouros-symbola-fonts

Then add the following to /etc/fonts/local.conf (system-wide) or ~/.fonts.conf (just for your user):

<fontconfig>
<match target="pattern">
 <test name="family"><string>Symbol</string></test>
 <edit name="family" mode="prepend" binding="same">
   <string>Symbola</string>
 </edit>
</match>
</fontconfig>

Now you should see this:

$ fc-match Symbol
Symbola.ttf: "Symbola" "Regular"

and symbols should render correctly.

同样,此解决方案归功于 Iñaki Ucar。

感谢所有提供此问题答案的人,感谢他们协助进行故障排除和促进此过程。希望这将在 R 核心本身的上游得到修复。