R语言学习 - 图形设置中英字体

时间:2022-04-26
本文章向大家介绍R语言学习 - 图形设置中英字体,主要内容包括修改图形的字体、ggplot2支持中文字体输出PDF、系统可用字体、合并字体支持中英文、一个示例、Reference、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

绘制生信宝典调查总结文中的柱状图时,出现了中文乱码,就搜索了下解决方案,记录如下。

修改图形的字体

ggplot2中修改图形字体。

# 修改坐标轴和legend、标题的字体
theme(text=element_text(family="Arial"))
# 或者
theme_bw(base_family="Arial")

# 修改geom_text的字体
geom_text(family="Arial")

ggplot2支持中文字体输出PDF

showtext包可给定字体文件,加载到R环境中,生成新的字体家族名字,后期调用这个名字设定字体,并且支持中文写入pdf不乱码

library(showtext)
showtext.auto(enable=TRUE)

font_path = "FZSTK.TTF"
font_name = tools::file_path_sans_ext(basename(font_path))
font.add(font_name, font_path)

# 修改坐标轴和legend、标题的字体
theme(text=element_text(family=font_name))

# 修改geom_text的字体
geom_text(family=font_name)

系统可用字体

  • Linux字体一般在 /usr/share/fonts下,也可以使用fc-list列出所以加载的字体。
  • Windows字体在 C:WindowsFonts下,直接可以看到,也可以拷贝到Linux下使用。

合并字体支持中英文

通常情况下,作图的字体都是英文,ggplot2默认的或按需求加载一种字体就可以了。但如果中英文混合出现时,单个字体只能支持一种文字,最好的方式是合并两种字体,类似于Word中设置中英文分别使用不同的字体。

软件FontForge可以方便的合并中英文字体,其安装也比较简单,直接 yum install fontforge.x86_64

假如需要合并FZSTK.TTF (windows下获取)和Schoolbell-Regular.ttf (谷歌下载),这两个都是手写字体。按如下,把字体文件和程序脚本mergefont.pe放在同一目录下,运行fontforge -script mergefont.pe即可获得合并后的字体FZ_School.ttf

$ ls
FZSTK.TTF mergefont.pe Schoolbell-Regular.ttf
$ cat mergefont.pe
Open("FZSTK.TTF")
SelectAll()
ScaleToEm(1024)
Generate("temp.ttf", "", 0x14)
Close()

# Open English font and merge to the Chinese font
Open("Schoolbell-Regular.ttf")
SelectAll()
ScaleToEm(1024)

MergeFonts("temp.ttf")
SetFontNames("FZ_School", "FZST", "Schoolbel", "Regular", "")
Generate("FZ_School.ttf", "", 0x14)
Close()

$ fontforge -script mergefont.pe
$ ls
FZ_School.ttf FZSTK.TTF mergefont.pe Schoolbell-Regular.ttf

然后安装前面的介绍使用showtext导入即可使用。

一个示例

字体文件自己从Windows获取,School bell从Google fonts获取。

library(showtext)
## Add fonts that are available on current path

# 方正字体+schoole bell (中英混合)
font.add("FZ_School", "FZ_School.ttf")
# 黑体
font.add("simhei", "simhei.ttf")
font.add("Arial","arial.ttf")

# 黑体和Arial的合体
font.add("HeiArial", "HeiArial.ttf")
showtext.auto()  ## automatically use showtext for new devices

library(ggplot2)

p = ggplot(NULL, aes(x = 1:10, y = 2^(1:10), group=1)) + geom_line() +
  theme(axis.title.y=element_text(family="Arial"), axis.title.x=element_text(family="HeiArial"), 
        plot.title=element_text(family="simhei")) +
  xlab("Days spent on 生信宝典") + 
  ylab("Things you have learned") +
  ggtitle("生信宝典,换个角度学生信") + 
  annotate("text", 7, 300, family = "FZ_School", size = 8,
           label = "收获曲线 (Harvest curve)", angle=15) 

# annotate指定的是文字的中间部分的位置

ggsave(p, filename="example-SXBD.pdf", width = 7, height = 4)  ## PDF device

Reference

  • 中英文字体混合 http://www.voidcn.com/article/p-gnggkwmy-vn.html
  • 改变字体类型 https://github.com/yixuan/showtext
  • 获取文件名 https://stackoverflow.com/questions/29113973/getting-filename-without-extension-in-r