Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

R Plotting - Chinese Support

R Plotting - Chinese Support

Font library directories on different systems:

/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

The fonts supported by the system can be viewed by installing showtext:

> install.packages("showtext", repos = "https://mirrors.ustc.edu.cn/CRAN/")  # 安装 showtext

...

> font_files()   # 查看字体

            path              file           family    face       version

1 /Library/Fonts Arial Unicode.ttf Arial Unicode MS Regular Version 1.01x

         ps_name

1 ArialUnicodeMS

Once you see ArialUnicodeMS, you can use it:



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

Load a custom font

The system font library is sometimes not well supported; the showtext() function can load our custom fonts. You can download a ttf font package and then add it using the font_add() function.

Here we use Source Han Sans, an open-source font released by Adobe and Google.

Official site: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, just pick one from there:

You can also download from cloud storage: https://pan.baidu.com/s/14cRhgYvvYotVIFkRVd71fQ 。

You can download an OTF font, such as SourceHanSansSC-Bold.otf, and place the file in the directory of the code being executed:

Use the font library for the bar chart:

Example

Load showtext

library

(

showtext

)

;

The first parameter sets the font name; the second is the font library path. In the same directory, we can just write the font library name.

font_add

(

"SyHei"

,

"SourceHanSansSC-Bold.otf"

)

;

Set the filename and output as png

png

(

file

=

"runoob-bar-cn.png"

)

cvd19

=

c

(

83534

,

2640626

,

585493

)

# Load font

showtext_begin

(

)

;

barplot

(

cvd19,

    main

=

"COVID-19 Bar Chart"

,

   

col

=

c

(

"#ED1C24"

,

"#22B14C"

,

"#FFC90E"

)

,

    names.

arg

=

c

(

"China"

,

"United States"

,

"India"

)

,

   

family

=

'SyHei'

   

Set the font library

)

Remove the font

showtext_end

(

)

;

Using Chinese in a 3D pie chart:

Example

library

(

plotrix

)

library

(

showtext

)

;

The first parameter sets the font name; the second is the font library path. In the same directory, we can just write the font library name.

font_add

(

"SyHei"

,

"SourceHanSansSC-Bold.otf"

)

;

Data preparation

info

=

c

(

1

,

2

,

4

,

8

)

Naming

names

=

c

(

"Google"

,

"Runoob"

,

"Taobao"

,

"Weibo"

)

Coloring (optional)

cols

=

c

(

"#ED1C24"

,

"#22B14C"

,

"#FFC90E"

,

"#3f48CC"

)

Set the filename and output as png

png

(

file

=

"3d_pie_chart.png"

)

# Load font

showtext_begin

(

)

;

Draw the 3D chart

pie3D

(

info,

labels

=

names

,explode

=

0.1

, main

=

"Let me test the SyHei font"

,

family

=

"SyHei"

)

Remove the font

showtext_end

(

)

;

Close the graphics device

dev.off

(

)

;