modelsummary 的 modelplot() 图中的 UTF-8 和 unicode

UTF-8 and unicode in modelsummary's modelplot() figures

我正在使用 R Markdown 创建 pdf 文档。 我想使用 modelsummary 包中包含的 modelplot() 函数在图中显示模型估计值和标准误差,但是如果图的图例包含日文字符,则会出现乱码。 The official documentation 没有提到如何处理日语。我该怎么做才能解决这个问题? 下面的图和代码是使用示例数据复制的。

model <- 
  list(
  `モデル1` = lm(mpg ~ ., data = mtcars),
  `モデル2` = lm(Sepal.Length ~ ., data = iris)
  )

modelplot(model)

这里是我设置的yaml的基本配置,用于文本的日文排版

output: 
  pdf_document:
    dev: cairo_pdf
    latex_engine: xelatex
documentclass: bxjsarticle
classoption: xelatex,ja=standard,a4paper,jafont=ms
header-includes: |
  \usepackage{zxjatype}

此外,要使用ggplot2输出图表,描述了以下设置。 如果包含此设置,使用 ggplot2 的图形将不会出现乱码,但前提是您使用 modelplot()。

library(fontregisterer)
library(systemfonts)
family_sans <- "MS Gothic" 
family_serif <- "MS Mincho" 
theme_set(
  theme_classic() +
  theme(
    text = element_text(family = family_serif, face = "plain"),
    title = element_text(face = "plain"),
    axis.title = element_text(face = "plain"),
    axis.title.x = element_text(face = "plain"),
    axis.title.y = element_text(face = "plain")
  )
)

我无法在我的 Linux 或 Mac 机器上重现此问题,因此这似乎是 Windows 特定的问题。 R 中的 UTF-8 和 unicode 支持是 notoriously finicky on Windows.

也就是说,至少在我自己的 Windows 机器上,下面的代码可以生成您想要的图表。诀窍是在 创建列表后 将模型名称分配给列表。

library(modelsummary)

model <- list(
    lm(mpg ~ ., data = mtcars),
    lm(Sepal.Length ~ ., data = iris))
names(model) <- c("モデル2", "モデル1")

modelplot(model)

上面的代码确实产生了警告,我不确定如何消除它。坦率地说,我不是编码专家,所以如果有人对此问题有见解,请在这里加入讨论:

https://github.com/vincentarelbundock/modelsummary/issues/345