R语言教程:中文支持

2022-08-0219:52:21编程语言入门到精通Comments1,216 views字数 2153阅读模式

不同系统的字体库目录:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26446.html

  • Linux 一般在 /usr/share/fonts 下,我们可以使用 fc-list 命令查看:
    # 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 字体在 C:\Windows\Fonts\ 文件下,直接打开就能看到了。
  • mac OS 字体在 /System/Library/Fonts 和 /Library/Fonts 目录下。

系统支持的字体库,可以通过安装 showtext 来查看:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26446.html

> 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

看到有 ArialUnicodeMS,我们就可以用了:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26446.html

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

载入自定义字体

系统的字体库有时候不是支持的很好, showtext() 函数可以载入我们自定义的字体,可以下载字体包 ttf,然后使用 font_add() 函数添加。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26446.html

这里我们使用思源黑体,思源黑体是 Adobe 与 Google 推出的一款开源字体。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26446.html

官网:https://source.typekit.com/source-han-serif/cn/文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26446.html

GitHub 地址:https://github.com/adobe-fonts/source-han-sans/tree/release/OTF/SimplifiedChinese文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26446.html

打开链接后,在里面选一个就好了:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26446.html

R语言教程:中文支持文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26446.html

你也可以在网盘下载: https://pan.baidu.com/s/14cRhgYvvYotVIFkRVd71fQ 。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26446.html

可以下载个 OTF 字体,比如 SourceHanSansSC-Bold.otf,将该文件文件放在当前执行的代码文件中:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26446.html

柱形图使用字体库:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26446.html

实例

# 载入 showtext
library(showtext);
# 第一个参数设置字体名称,第二个参数为字体库路径,同目录下,我们写字体库名就可以了
font_add("SyHei", "SourceHanSansSC-Bold.otf");文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26446.html

# 设置文件名,输出为 png
png(file = "runoob-bar-cn.png")文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26446.html

cvd19 = c(83534,2640626,585493)
#加载字体
showtext_begin();
barplot(cvd19,
main="新冠疫情条形图",
col=c("#ED1C24","#22B14C","#FFC90E"),
names.arg=c("中国","美国","印度"),
family='SyHei'     # 设置字体库
)
# 去掉字体
showtext_end();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26446.html

R语言教程:中文支持文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26446.html

3D 饼图使用中文:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26446.html

实例

library(plotrix)
library(showtext);
# 第一个参数设置字体名称,第二个参数为字体库路径,同目录下,我们写字体库名就可以了
font_add("SyHei", "SourceHanSansSC-Bold.otf");
# 数据准备
info = c(1, 2, 4, 8)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26446.html

# 命名
names = c("Google", "Runoob", "Taobao", "Weibo")文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26446.html

# 涂色(可选)
cols = c("#ED1C24","#22B14C","#FFC90E","#3f48CC")文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26446.html

# 设置文件名,输出为 png
png(file = "3d_pie_chart.png")文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26446.html

#加载字体
showtext_begin();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26446.html

# 绘制 3D 图
pie3D(info,labels = names,explode = 0.1, main = "我测试一下 SyHei 字体",family = "SyHei")文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26446.html

# 去掉字体
showtext_end();
# 关闭图形设备
dev.off();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26446.html

R语言教程:中文支持文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26446.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/26446.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定