显示文本 font_add_google 中未加载字体

sna*_*ack 5 r ggplot2 showtext

我正在尝试绘制一些数据的图表,我的代码如下所示:

library('ggplot2')
library('tidyr')
library('ggthemes')
library('showtext')

font_add_google('Syncopate', 'Syncopate')
showtext_auto()

ggplot(aes(x = X, group=1), data = glassdoor)+
  geom_line(aes(y = col1, color = 'red'))+
  geom_line(aes(y = col2, color = 'blue'))+
  geom_line(aes(y = col3, color = 'magenta'))+
  geom_line(aes(y = col4, color = 'yellow'))+
  theme(text = element_text(family = "Syncopate"))+
  ggtitle('A Long Test Title')
Run Code Online (Sandbox Code Playgroud)

切分音是一种独特的字体,请参见此处。但我的可视化字体看起来像这样(这是一个测试图,忽略其整体的糟糕性):

在此输入图像描述

但如果我加载像 Times New Roman 这样的系统主题,它就可以正常工作。为什么我的谷歌字体不使用 showtext 加载?

编辑

Jrakru 的答案有效,但请记住,您必须运行整个代码块:新字体将出现在保存的 png 文件中,但不会出现在预览窗口中。这并不是对答案的轻微反对,而是针对像我这样希望字体显示在 RStudio 控制台中的其他人,因此省略了代码的ggsave和部分。png

Jra*_*u56 6

showtext提及的 GitHub

此示例应该适用于大多数图形设备,包括 pdf()、png()、postscript() 和屏幕设备,例如 Windows 上的 windows() 和 Linux 上的 x11()。

如果您真的非常认真地阅读行间内容,则意味着该RStudioGD图形设备不受支持。前几次读的时候我并没有看到这一点。我只知道因为它vignette更明确一点。

注意:目前 showtext 不适用于 RStudio 的内置图形设备,因此要尝试下面的代码,建议在原始 R 控制台中运行代码,或使用其他图形设备,例如 x11() 和 windows()

请参阅 https://cran.rstudio.com/web/packages/showtext/vignettes/introduction.html

有了上面的知识,我们可以这样做:

library('tidyr')
library('ggthemes')
library('showtext')

font_add_google("Schoolbell", "bell")
showtext_auto()

library('ggplot2')

df<- data.frame(x=1:10, y=101:110)

options("device" = "windows")

win.graph(10,10,12)

ggplot(data = df) +
  geom_line(aes(x,y))+
  theme(text = element_text(family = "bell"))+
  ggtitle('A Long Test Title')


ggsave("showtext-example.png", width = 7, height = 4, dpi = 96)

options("device" = "RStudioGD")
Run Code Online (Sandbox Code Playgroud)

瞧! 在此输入图像描述

PS:我以为你是windows用户。