我想(并且曾经)在 Windows、Mac 和 Linus 上使用 ggplot 的自定义字体(shinyapps.io,请参阅 here )。这是不久前的事了,不知何故,它不再起作用了..
我详细尝试过的内容:
第 1 步:初始字体可用性..
windowsFonts()
$serif
[1] "TT Times New Roman"
$sans
[1] "TT Arial"
$mono
[1] "TT Courier New"
第 2 步:添加 Google 字体
sysfonts::font_add_google("Permanent Marker")
第 3 步:确保字体已注册。
sysfonts::font_families()
[1] "sans" "serif" "mono" "Permanent Marker"
windowsFonts()
$serif
[1] "TT Times New Roman"
$sans
[1] "TT Arial"
$mono
[1] "TT Courier New"
为什么 sysfonts
可以看到并显示自定义字体,而 windowsFonts
却看不到?
第 4 步:尝试在绘图中使用自定义字体:
使用serif
有效:
ggplot(mtcars) +
geom_point(aes(wt, mpg)) +
theme(text = element_text(family = "serif"))
使用永久标记
不会:
ggplot(mtcars) +
geom_point(aes(wt, mpg)) +
theme(text = element_text(family = "Permanent Marker"))
我错过了什么?
最佳答案
一种方法是通过 showtext
包。详细流程描述here和 there .
基本上,我们需要使用 showtext_auto()
以便 sysfonts
字体可用于 ggplot
。
一个最小的例子:
# initial configuration
windowsFonts()
$serif
[1] "TT Times New Roman"
$sans
[1] "TT Arial"
$mono
[1] "TT Courier New"
# load new font
sysfonts::font_add_google("Permanent Marker")
sysfonts::font_families()
[1] "sans" "serif" "mono"
[4] "Permanent Marker"
# use it
showtext_auto()
ggplot(mtcars) +
geom_point(aes(wt, mpg)) +
theme(text = element_text(family = "Permanent Marker"))
关于r - 在 Windows 10 上的 R 中使用谷歌字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70594193/