English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

R Plotting Chinese Support

Font library directory of different systems:

  • Linux is generally located in /usr/share/fonts Below, we can use fc-list command to view:

    # fc-list
    /usr/share/fonts/truetype/dejavu/DejaVuSerif-Bold.ttf: DejaVu Serif:style=Bold
    /usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf: DejaVu Sans Mono:style=Book
    /usr/share/fonts/truetype/dejavu/DejaVuSans.ttf: DejaVu Sans:style=Book
    /usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf: DejaVu Sans:style=Bold
    /usr/share/fonts/truetype/dejavu/DejaVuSansMono-Bold.ttf: DejaVu Sans Mono:style=Bold
    /usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf: DejaVu Serif:style=Book
  • Windows fonts are in C:\Windows\Fonts\ files, and they can be directly opened to see them.

  • mac OS fonts are in /System/Library/Fonts and /Library/Fonts directory.

The system-supported font library can be viewed by installing showtext:

> install.packages("showtext", repos = "https://mirrors.ustc.edu.cn/CRAN/)) # Install showtext
...
> font_files() # View fonts
            path file family face version
1 /Library/Fonts Arial Unicode.ttf Arial Unicode MS Regular Version 1.01x
         ps_name
1 ArialUnicodeMS

Since we see ArialUnicodeMS, we can use it:

pie3D(info, labels = names, explode = 0.1, main = ""3D  Figure ", family = "ArialUnicodeMS")

Load custom font

Sometimes the system's font library is not supported very well, The showtext() function can load our custom fonts, and you can download the font package ttf and then add it using the font_add() function.

Here we use Source Han Sans, which is an open-source font jointly launched by Adobe and Google.

Official website:https://source.typekit.com/source-han-serif/cn/

GitHub address:https://github.com/adobe-fonts/source-han-sans/tree/release/OTF/SimplifiedChinese

After opening the link, select one from within it:

You can download an OTF font, such as SourceHanSansSC-Bold.otf, place the file in the current executing code file:

Bar Chart with Font Library:

# Load showtext
library(showtext);
# The first parameter sets the font name, the second parameter is the font library path, in the same directory, we just write the font library name
font_add("SyHei", "SourceHanSansSC-Bold.otf"); 
# Set the filename, output as png
png(file = "w3codebox-bar-cn.png")
cvd19 = c(83534,2640626,585493)
# Load the font
showtext_begin();
barplot(cvd19,
    main="COVID-19 Bar Chart",
    col=c("#ED1C24","22B14C","#FFC90E"),
    names.arg=c("China", "America", "India"),
    family='SyHei' # Set the font library
)
# Remove the font
showtext_end();

3D Pie Chart with Chinese:

library(plotrix)
library(showtext);
# The first parameter sets the font name, the second parameter is the font library path, in the same directory, we just write the font library name
font_add("SyHei", "SourceHanSansSC-Bold.otf");
# Data preparation
info = c(1, 2, 4, 8)
# Naming
names = c("Google", "w3codebox", "Taobao", "Weibo"
# Coloring (optional)
cols = c("#ED1C24","22B14C","#FFC90E","#3f48CC"
# Set the filename, output as png
png(file = ""3d_pie_chart.png")
# Load the font
showtext_begin();
# Plot 3D figure
pie3D(info, labels = names, explode = 0.1, main = ""3D figure, family = "SyHei"
# Remove the font
showtext_end();
# Close the graphics device
dev.off();