更改 ggplot2 中的字体 [英] Changing fonts in ggplot2
问题描述
曾几何时,我使用 windowsFonts(Times=windowsFont("TT Times New Roman"))
更改了我的 ggplot2
字体.现在我无法摆脱这个.
Once upon a time, I changed my ggplot2
font using using windowsFonts(Times=windowsFont("TT Times New Roman"))
to change it. Now I can't get it off of this.
在尝试在 ggplot2
theme()
中设置 family=""
时,我似乎无法像我一样生成字体更改使用不同的字体系列编译下面的 MWE.
In trying to set family=""
in ggplot2
theme()
I can't seem to generate a change in fonts as I compile the MWE below with different font families.
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"))
# family="CM Roman"))
# family="TT Times New Roman"))
# family="Sans"))
family="Serif"))
print(a)
print("Graph should have refreshed")
R 返回警告 font family not found in Windows font database
,但是我正在关注一个教程(如果我能再次找到它,我会在这里更新链接)说这是正常,不是问题.此外,不知何故这在某一时刻起作用了,因为我的图表曾经使用过一些 arial 或 helvitica 字体.我认为即使在最初的迁移过程中,这也一直是一个当前的警告.
R is returning a warning font family not found in Windows font database
, but there was a tutorial I was following (if I can find it again I will update the link here) that said this was normal and not a problem. Also, somehow this worked at one point because my graph once used some arial or helvitica type font. I think this has always been a present warning even during the initial times migration.
更新
当我运行 windowsFonts()
我的输出是
when I run windowsFonts()
my output is
$serif [1] "TT Times New Roman"
$serif [1] "TT Times New Roman"
$sans [1] "TT Arial"
$sans [1] "TT Arial"
$mono [1] "TT Courier New"
$mono [1] "TT Courier New"
但是,这是在我运行 font_import()
之后,所以我只能得出结论,我的字体没有保存在正确的位置.运行 font_import()
请求的代码实际上加载了库:
But, this is after I ran font_import()
so I can only conclude that my fonts are not being saved in the right place. The code that ran the font_import()
request actually loads the libraries with:
LocalLibraryLocation <- paste0("C:\Users\",Sys.getenv("USERNAME"),"\Documents","\R\win-library\3.2");
.libPaths(c(LocalLibraryLocation, .libPaths()))
推荐答案
我认为你只是错过了一个初始化步骤.
You just missed an initialization step I think.
您可以使用命令 windowsFonts()
查看可用的字体.例如,当我开始看这个时,我的看起来像这样:
You can see what fonts you have available with the command windowsFonts()
. For example mine looks like this when I started looking at this:
> windowsFonts()
$serif
[1] "TT Times New Roman"
$sans
[1] "TT Arial"
$mono
[1] "TT Courier New"
在安装包 extraFont 并像这样运行 font_import
之后(花了大约 5 分钟):
After intalling the package extraFont and running font_import
like this (it took like 5 minutes):
library(extrafont)
font_import()
loadfonts(device = "win")
我有更多可用的 - 可以说是太多了,当然太多了,无法在此列出.
I had many more available - arguable too many, certainly too many to list here.
然后我尝试了你的代码:
Then I tried your code:
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)
让步:
您可以使用以下代码段找到 element_text
的 family
参数所需的字体名称:
You can find the name of a font you need for the family
parameter of element_text
with the following code snippet:
> 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)
产量:
这篇关于更改 ggplot2 中的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!