6

我已经安装了“extrafont”包,以便通过 ttf_import() 方法安装外部字体库Duality 。但是,当通过 wordcloud 方法指定字体时,我收到以下错误:

安装命令:

# Assuming the font file, DUALITY_.ttf, is in the working directory (see link to font above)
font_import(".",FALSE,pattern="DUALITY")

词云命令:

wordcloud(ap.d$word, ap.d$freq, scale=c(8,2), min.freq=10, vfont=c("Duality","plain"),
      random.order=FALSE, rot.per=0, use.r.layout=FALSE, colors=pal2, fixed.asp=FALSE)

输出:

Error in strwidth(words[i], cex = size[i], ...) : 
  invalid 'vfont' value [typeface -2147483648]

为了验证确实安装了字体,我发出了以下命令

> choose_font("Duality")
[1] "Duality"
> fonts()
....[49] "Waree"                    "Duality"    

为什么对偶字体对 wordcloud 的 vfont 参数不可见?以及如何使其对 Cairo(默认渲染器)可见。蒂亚!

4

2 回答 2

4

我已经能够使用传递给文本和描述的参数来克服同样的问题familyfont?par不是vfont. 我还需要先加载字体。事情是这样的:

导入字体(抱歉,OP 中提供的 Duality 的链接不再可用,我使用 Lucida Handwriting 代替,在 windows 中可用):

library(extrafont)
font_import(pattern="LHANDW")

加载(有关详细信息,请参阅此博客):

loadfonts() # loadfonts(device = "win") if you are working in windows

词云:

wordcloud(ap.d$word, ap.d$freq, scale=c(8,2), min.freq=10, family="Lucida Handwriting", font=1,
  random.order=FALSE, rot.per=0, use.r.layout=FALSE, colors=pal2, fixed.asp=FALSE)
于 2016-01-12T21:07:03.830 回答
1

为了补充以前的答案,并解释如何实际选择要使用的字体。一、导入字体(可以在中设置不同于默认的路径font_import()

library(extrafont)
font_import(prompt = FALSE)

要了解可用的字体:

unique(fonttable()$FamilyName)

这提供了包含“字体系列”的确切参考。然后,您可以发出这样的wordcloud命令:

wordcloud(c(letters, LETTERS, 0:9), seq(1, 1000, len = 62), family = "Carlito", font = 1)

为什么font = 1?从?par(),这是关于font参数的内容:

一个整数,它指定用于文本的字体。如果可能,设备驱动程序会安排 1 对应纯文本(默认),2 对应粗体,3 对应斜体,4 对应粗斜体。

于 2018-01-10T19:12:54.523 回答