我认为您刚刚错过了初始化步骤。
您可以使用以下命令查看可用的字体windowsFonts()
。例如,当我开始查看这个时,我的看起来像这样:
> windowsFonts()
$serif
[1] "TT Times New Roman"
$sans
[1] "TT Arial"
$mono
[1] "TT Courier New"
安装extraFont包并运行后font_import
像这样(大约花了5分钟):
library(extrafont)
font_import()
loadfonts(device = "win")
我还有更多可用的东西——可以说太多了,当然太多了,无法在这里列出。
然后我尝试了你的代码:
library(ggplot2)
library(extrafont)
loadfonts(device = "win")
a <- 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"))
print(a)
产生这个:
Update:
您可以找到您需要的字体名称family
的参数element_text
使用以下代码片段:
> names(wf[wf=="TT Times New Roman"])
[1] "serif"
进而:
library(ggplot2)
library(extrafont)
loadfonts(device = "win")
a <- 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="serif"))
print(a)
yields:
![enter image description here](https://i.stack.imgur.com/op4io.png)