首页 > 解决方案 > 在 ggplot2 中使用来自 Google 的 sysfonts

问题描述

我试图弄清楚如何使用sysfonts包(或其他方法)将 Google 字体添加到 ggplot2。我可以很容易地使用它们,extrafont但是有人有工作流程可以添加它们sysfonts吗?以下是我到目前为止的内容:

library(ggplot2)
library(extrafont)
#> Registering fonts with R
#font_import() ## takes a while to run
loadfonts(device = "win")
#> Agency FB already registered with windowsFonts().
#> Algerian already registered with windowsFonts().
...
#> Vivaldi already registered with windowsFonts().
#> Vladimir Script already registered with windowsFonts().
#> Webdings already registered with windowsFonts().
#> Wingdings already registered with windowsFonts().
#> Wingdings 2 already registered with windowsFonts().
#> Wingdings 3 already registered with windowsFonts().


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="Lucida Console"))



library(sysfonts)
font_add_google('Yanone Kaffeesatz')

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="Yanone Kaffeesatz"))
#> Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
#> found in Windows font database

#> Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
#> found in Windows font database

#> Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
#> found in Windows font database
#> Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
#> family not found in Windows font database

#> Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
#> family not found in Windows font database

#> Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
#> family not found in Windows font database

#> Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
#> family not found in Windows font database

#> Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
#> family not found in Windows font database

#> Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
#> family not found in Windows font database

#> Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
#> family not found in Windows font database

#> Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
#> family not found in Windows font database

#> Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
#> family not found in Windows font database

#> Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
#> family not found in Windows font database
#> Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
#> font family not found in Windows font database
#> Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
#> family not found in Windows font database

reprex 包(v0.3.0)于 2020 年 2 月 28 日创建

显然第二个情节不是用谷歌字体创建的。有任何想法吗?

标签: rggplot2google-webfonts

解决方案


推荐阅读