【3.3】ggplot字体的修改

今天遇到了一个奇怪的现象,ggplot再不同服务器做出来的图的字体居然不一样。没办法,为了保证字体的一致性,需要统一一下字体,默认为arial吧。

install.packages("extrafont")
library(extrafont)

指定字体,如果不指定pattern,则默认倒入所有的字体

font_import(pattern="[C/c]omic")
font_import(pattern="[A/a]rial")

你可以看一下目前倒入的字体

fonts()
fonttable()

Alright, if you want to use the fonts to display graphs on-screen (what you normally want to do, e.g. in the plots-window in RStudio), you have to load the fonts to the Windows device:

loadfonts(device="win")

然后就可以做图了

library(ggplot2)
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() +
 ggtitle("Fuel Efficiency of 32 Cars") +
 xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") +
 theme(text=element_text(size=16, family="Comic Sans MS"))

参考资料:

https://www.r-bloggers.com/change-fonts-in-ggplot2-and-create-xkcd-style-graphs/

药企,独角兽,苏州。团队长期招人,感兴趣的都可以发邮件聊聊:tiehan@sina.cn
个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn