0

I am using R Markdown to create a pdf document. I would like to use the modelplot() function included in the modelsummary package to show the model estimates and standard errors in a figure, but if the legend of the figure contains Japanese characters, it will be garbled. The official documentation did not mention how to handle Japanese. What can I do to solve this problem? The figure and code below are reproduced using sample data.

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

modelplot(model)

Figure output using modelplot()

Here is the basic configuration of yaml that I have set up for the Japanese typesetting of the text.

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

Also, to output diagrams using ggplot2, the following settings are described. If you include this setting, figures using ggplot2 will be output without garbling, but only if you use 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")
  )
)
Vincent
  • 15,809
  • 7
  • 37
  • 39
ryoto
  • 361
  • 1
  • 10
  • 2
    Could a work around be saving as a png and then reading the png back into the Rmd documentn (using `knitr::include_graphic()`? – Tob Aug 09 '21 at 12:40

1 Answers1

1

I cannot replicate this problem on my Linux or Mac machines, so this appears to be a Windows-specific issue. UTF-8 and unicode support in R is notoriously finicky on Windows.

That said, on my own Windows machine at least, this code below produces the graph you want. The trick is to assign model names to the list after creating the list.

library(modelsummary)

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

modelplot(model)

The code above does produce a warning, and I'm not sure how to get rid of it. Frankly, I am not an encoding expert, so if anyone has insight into this issue, please join the disscussion here:

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

Vincent
  • 15,809
  • 7
  • 37
  • 39
  • 1
    Thank you for your answer. I tried the two different methods suggested. The method Vincent suggested could not be reproduced. I'm not sure why, but as he mentions, it may be Windows specific or there may be a Japanese language issue. In fact, my machine is Windows10 in a Japanese environment. I should have mentioned this at the beginning. The method suggested by Tod was successful in avoiding the garbled text. The image is not vector, and the font is different from other images, but I could read the text well enough. I found both of their suggestions very helpful. Thank you very much. – ryoto Aug 09 '21 at 16:57