如何调整R语言绘图的字体

R语言绘图字体的设置方案主要有两种:

  • windowsFont()函数和windowsFonts()函数

  • sysfonts, showtext等第三方包

1. windowsFont(s)()函数

用法如下:

1
2
windowsFont(family = "") # 加载系统中已存在的某个字体
windowsFonts(huawenxinwei = windowsFont(family = "STXinwei")) # 对这些字体进行重命名,以便R能够直接使用

这里是常见的中英文字体的中英文对照表

字体中文名 字体英文名
黑体 SimHei
宋体 SimSun
新宋体 NSimSun
仿宋 FangSong
楷体 KaiTi
仿宋_GB2312 FangSong_GB2312
楷体_GB2312 KaiTi_GB2312
微软正黑体 Microsoft JhengHei
微软雅黑 Microsoft YaHei
新细明体 PMingLiU
细明体 MingLiU
标楷体 DFKai-SB
隶书 LiSu
幼圆 YouYuan
华文细黑 STXihei
华文楷体 STKaiti
华文宋体 STSong
华文中宋 STZhongsong
华文仿宋 STFangsong
方正舒体 FZShuTi
方正姚体 FZYaoti
华文彩云 STCaiyun
华文琥珀 STHupo
华文隶书 STLiti
华文行楷 STXingkai
华文新魏 STXinwei

windows系统常见的英文字体有:

英文字体 说明
Arial 常用
Times New Roman 常用于学术期刊
Lucida Caligraphy 漂亮的手写体
Helvetica 适合阅读
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
windowsFonts(
# 中文字体
lishu = windowsFont(family = "LiSu"), # 隶书
yahei = windowsFont(family = "Microsoft YaHei"), # 微软雅黑
xinwei = windowsFont(family = "STXingwei"), # 华文新魏
kaiti = windowsFont(family = "KaiTi"), # 楷体
heiti = windowsFont(family = "SimHei"), # 黑体
# 英文字体
arial = windowsFont(family = "Arial"), # Arial字体
newman = windowsFont(family = "Times New Roman"), #Times New Roman字体
hand = windowsFont(family = "Lucida Calligraphy"), # Lucida手写体
Helvetica = windowsFont(family = "Helvetica") # 印刷体
)
ggplot(mtcars) +
geom_point(aes(x = mpg, y = drat)) +
# 中文字体
annotate(geom = "text", x = 15, y = 4.5, label = "隶书", size = 18, color = "tomato2", family = "lishu") +
annotate(geom = "text", x = 26, y = 4.5, label = "微软雅黑", size = 18, color = "tomato2", family = "yahei") +
annotate(geom = "text", x = 15, y = 3.8, label = "楷体", size = 18, color = "tomato2", family = "kaiti") +
annotate(geom = "text", x = 25, y = 3.8, label = "黑体", size = 18, color = "tomato2", family = "heiti") +
annotate(geom = "text", x = 18, y = 3.0, label = "华文新魏", size = 18, color = "tomato2", family = "xinwei") +
# 英文字体
annotate(geom = "text", x = 18, y = 3.5, label = "Arial Font", size = 12, color = "tomato2", family = "arial") +
annotate(geom = "text", x = 20, y = 4.8, label = "Times New Roman", size = 12, color = "tomato2", family = "newman") +
annotate(geom = "text", x = 30, y = 3.0, label = "Lucida", size = 12, color = "tomato2", family = "hand") +
annotate(geom = "text", x = 30, y = 3.5, label = "Helvetica", size = 12, color = "tomato2", family = "Helvetica")

2. showtext

下面以showtext包中自带的例子加以说明。

目前Rstudio包中的默认绘图设备并不支持showtext包,所以想要得到类似下图的效果,请直接在R控制台中操作或者打开x()/windows()绘图设备

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
library(showtext)
## Loading Google fonts (http://www.google.com/fonts)
font_add_google("Gochi Hand", "gochi")
font_add_google("Schoolbell", "bell")
## Automatically use showtext to render text
showtext_auto()
set.seed(123)
## Manually open a graphics device if you run this code in RStudio
## x11()
hist(rnorm(1000), breaks = 30, col = "steelblue", border = "white",
main = "", xlab = "", ylab = "")
title("Histogram of Normal Random Numbers", family = "bell", cex.main = 2)
title(ylab = "Frequency", family = "gochi", cex.lab = 2)
text(2, 70, "N = 1000", family = "bell", cex = 2.5)

具体的用法

  • (*) 加载字体

  • 打开绘图设备

  • (*) 申明使用showtext包来渲染字体

  • 绘图

  • 关闭绘图设备

如果想要全局应用showtext包,那么应该使用showtext_auto()函数

载入字体

1
font_add(family = "<family_name>", regular = "/path/to/font/file") # regurlar即字体的路径
1
2
3
4
5
6
7
8
library(showtext)
#> Loading required package: sysfonts
#> Loading required package: showtextdb
## 载入黑体
font_add("heiti", regular = "simhei.ttf")
## 载入Constantia字体
font_add("constan", regular = "constan.ttf", italic = "constani.ttf")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
showtext_auto()
library(ggplot2)
p <- ggplot(NULL, aes(x = 1, y = 1)) +
ylim(0.8, 1.2) +
theme(
axis.title = element_blank(), axis.ticks = element_blank(),
axis.text = element_blank()
) +
annotate(
"text", 1, 1.1, family = "heiti", size = 15,
label = "你好,世界!"
) +
annotate(
"text", 1, 0.9, label = 'Chinese for "Hello, world!"',
family = "constan", fontface = "italic", size = 12
)
## PNG设备
ggsave("showtext-example-4.png", width = 7, height = 4, dpi = 96)
## 关闭showtext字体设置
showtext_auto(FALSE)

参考