Fix Chinese Characters in R Language
The inability to display Chinese characters in R language is usually due to the default encoding of the system or software not supporting Chinese characters. The solution is as follows:
- Use UTF-8 encoding: Add the following code at the beginning of the R script to set the encoding to UTF-8.
options(encoding = "UTF-8")
- additional typeface
- additional typeface
install.packages("extrafont")
After that, execute the following code:
library(extrafont)
font_import()
loadfonts()
- Use Chinese fonts: Specify to use a font that includes Chinese characters when drawing, such as Microsoft YaHei or SimSun.
par(family = "微软雅黑")
plot(1:10, main = "中文标题")
By following the above methods, the issue of R language not displaying Chinese characters should be resolved.