在 R 中更改图形的字体 [英] Changing Fonts for Graphs in R
问题描述
在我的研究中,我使用 R 生成了各种图形.我看到大多数图形都带有各种大小的 Sans Serif 字体.
如何将图形中的所有文本(x 标签、y 标签、标题、图例等)更改为统一字体,例如Times New Roman,12pt,粗体?
您可以使用 和 答案,其中描述了创建 xkcd 样式图,使用 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屋!