r - 在 Windows 10 上的 R 中使用谷歌字体

标签 r ggplot2 google-fonts

我想(并且曾经)在 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"))

enter image description here

使用永久标记不会:

ggplot(mtcars) + 
  geom_point(aes(wt, mpg)) + 
  theme(text = element_text(family = "Permanent Marker"))

enter image description here

我错过了什么?

最佳答案

一种方法是通过 showtext 包。详细流程描述herethere .

基本上,我们需要使用 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"))

enter image description here

关于r - 在 Windows 10 上的 R 中使用谷歌字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70594193/

相关文章:

r - 如何在R中编写trycatch

r - 扩展 RColorBrewer 以支持更多颜色?

javascript - 仅从 R 和 htmlwidgets 保存 html 文件很热?

r - 使用 stat_summary 在线图上的形状

r - 如何在 ggplot2 中绘制虚线周围的轮廓或边框?

r - 如何将直方图和密度图与 Y 轴上的数字而不是密度重叠

html - 谷歌字体显示=交换奇怪的行为

html - 特定网页在 Firefox 上呈现的字体不同

r - 尝试使用 RSelenium 检测并填充表单