绘制生信宝典调查总结文中的柱状图时,出现了中文乱码,就搜索了下解决方案,记录如下。
修改图形的字体
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)
系统可用字体
合并字体支持中英文
通常情况下,作图的字体都是英文,ggplot2默认的或按需求加载一种字体就可以了。但如果中英文混合出现时,单个字体只能支持一种文字,最好的方式是合并两种字体,类似于Word中设置中英文分别使用不同的字体。
软件FontForge可以方便的合并中英文字体,其安装也比较简单,直接 yum install fontforge.x86_64。
假如需要合并FZSTK.TTF (windows下获取)和Schoolbell-Regular.ttf (谷歌下载),这两个都是手写字体。按如下,把字体文件和程序脚本mergefont.pe放在同一目录下,运行fontforge -script mergefont.pe即可获得合并后的字体FZ_School.ttf。
$ lsFZSTK.TTF mergefont.pe Schoolbell-Regular.ttf$ cat mergefont.peOpen("FZSTK.TTF")SelectAll()ScaleToEm(1024)Generate("temp.ttf", "", 0x14)Close()# Open English font and merge to the Chinese fontOpen("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$ lsFZ_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 deviceslibrary(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
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!