字体未在showtext中加载font_add_google [英] Fonts not loading in showtext font_add_google

查看:209
本文介绍了字体未在showtext中加载font_add_google的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to graph some data and my code looks like this:

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')

Syncopate是一种独特的字体,在此处中可以看到.但是我的可视化字体看起来像这样(这是一个测试图,请忽略其整体效果):

Syncopate is a distinctive font, seen here. But my visualization's font just looks like this (this is a test graph, ignore its overall poorness):

但是,如果我加载诸如Times New Roman之类的系统主题,则效果很好.为什么我的Google字体不使用showtext加载?

But if I load a system theme like Times New Roman, it works fine. Why aren't my google fonts loading using showtext?

修改

Jrakru的答案有效,但请记住,您必须运行整个代码块:新字体将显示在已保存的png文件中,而不显示在预览窗口中.这并不是为了解决问题而写的,而是针对像我这样的人,他们希望字体显示在RStudio控制台中,因此省略了代码的ggsavepng部分.

Jrakru's answer works, but bear in mind that you have to run that entire code block: The new fonts will appear in a saved png file, but not in the preview window. This isn't written as a slight against the answer, but rather for others like myself who expect the fonts to show up in the RStudio console and therefore omit the ggsave and png portions of the code.

推荐答案

showtext的GitHub提及

此示例应可在大多数图形设备上使用,包括pdf(), png(),postscript()和屏幕上的设备,例如Windows() Windows和Linux上的x11().

This example should work on most graphics devices, including pdf(), png(), postscript(), and on-screen devices such as windows() on Windows and x11() on Linux.

如果您在两行之间阅读《 真的很辛苦》,则表示不支持RStudioGD图形设备.最初的几次我没有看到.我只知道,因为vignette更加明确.

If you read really really hard between lines, that means, that RStudioGD graphics device is not supported. I did not see that the first few times I read it. I only know because the vignette is a little more explicit.

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

NOTE: Currently showtext does not work with the built-in graphics device of RStudio, hence to try the code below, it is suggested to run the code in original R console, or use other graphics devices such as x11() and windows()

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

有了以上知识,我们可以做到这一点:

With the above knowledge, we can do this:

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")

瞧,瞧!

Ps:我假设您是windows用户.

Ps: I assumed you are a windows user.

这篇关于字体未在showtext中加载font_add_google的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆