最近使用R画图遇到两个问题
1. 使用其它字体
2. 保存 plot 到 pdf 时将字体嵌入pdf (embed the font)
使用extrafont和Ghostscript可以解决这两个问题。
1. Ghostscript
安装: http://www.ghostscript.com/download/gsdnld.html
2. extrafont (R package)
以下命令都是在R中运行
- 安装
> install.packages("extrafont")
> library(extrafont)
> #这一步定位系统字体,比如C:\Windows\Fonts\*.ttf, 需要几分钟的时间
> font_import()
> loadfonts()
- 使用
> #查看可用的font family
> fonts()
> library(ggplot2)
> #使用字体Times New Roman画图
> p <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() +
xlab("Weight") + ylab("Miles per Gallon") +
theme(text=element_text(family="Times New Roman"))
> #保存pdf
> ggsave