rplotencodingutf-8chinese-locale

Can't display Chinese characters in R plot


Chinese characters don't display in a basic R plot:

plot(c(1,2), c(1,3), xlab = "基本", ylab = "收入")

I tried adding family = 'Arial Unicode MS' to plot, and I tried setting Sys.setlocale("LC_ALL", "zh_CN.UTF-8") or Sys.setlocale("LC_ALL", "en_US.UTF-8") beforehand, each time I get the same plot: enter image description here

I use R 4.3.1 and RStudio 2023.09.0 (Build 463) on a Windows 11 PC.

Any idea how to solve this?


Solution

  • It depends on your system and the graphics device that you are using. The ragg package has graphics devices that do a good job on most fonts on most systems, but they mostly write to a file, they don't display on screen.

    RStudio has support for on-screen displays using ragg: see https://ragg.r-lib.org/#use-ragg-in-rstudio . With that enabled you should see your full character sets, provided you have the right fonts installed on your system.

    Unfortunately, ragg fails on a few font/style combinations, and will leave the characters blank. If this happens, a workaround is to specify a different font family. For example, according to the comments the default font failed, but plot(c(1,2), c(1,3), xlab = "基本", ylab = "收入", family="Microsoft JhengHei") worked. This is a known bug, but I don't know when it will be fixed.