在R中更改图形的字体 [英] Changing Fonts for Graphs in R

查看:1502
本文介绍了在R中更改图形的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的研究中,我使用R生成各种图形。我发现大多数图形都带有各种大小的Sans Serif字体。

如何将图表中的所有文字(x标签,y标签,标题,图例等)更改为统一的字体,例如Times New Roman,12pt,Bold?

解决方案

您可以使用答案,其中描述了如何创建<使用 extrafont 的风格图形 包。




In my study I am generating various graphs using R. I see that most of the graphs come up with a Sans Serif type font with various sizes.

How to I change all the text in a graph (x-label, y-label, title, legend etc.) into a uniform font e.g. Times New Roman, 12pt, Bold?

解决方案

You can use the extrafont package.

install.packages("extrafont")
library(extrafont)
font_import()
loadfonts(device="win")       #Register fonts for Windows bitmap output
fonts()                       #vector of font family names
##  [1] "Andale Mono"                  "AppleMyungjo"                
##  [3] "Arial Black"                  "Arial"                       
##  [5] "Arial Narrow"                 "Arial Rounded MT Bold"  

library(ggplot2)
data(mtcars)
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() +     
  ggtitle("Fuel Efficiency of 32 Cars") +
  xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") +
  theme_bw() +
  theme(text=element_text(family="Times New Roman", face="bold", size=12)) #Times New Roman, 12pt, Bold
#example taken from the Github project page

Note: Using the extrafont package, you can also embed these fonts in PDF and EPS files (make plots in R and export to PDF/EPS). You can also directly create math symbols (see math equation in plot below), usually created using TeX. More information here and here. Also look at the github project page.

Also look at this answer which describes creating xkcd style graphs using the extrafont package.

这篇关于在R中更改图形的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆