R wordcloud external ttf vfont not recognized

I installed the "extrafont" package to install the external Duality font library using the ttf_import () method. However, when specifying a font using the wordcloud method, I get the following error:

Installation command:

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

Wordcloud Command:

 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) 

Output:

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

To make sure the font is really installed, I issued the following commands

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

Why is the Duality font not showing up in the vfont parameter of wordcloud? And how to make it visible to Cairo (the default rendering tool). TIA!

+4
source share
2 answers

I was able to overcome the same problem using the parameters passed to the text family and font and described in ?par instead of vfont . Also, I first needed to download the font. So the thing goes:

Import the font (sorry, the Duality link provided in OP is no longer available, instead I use Lucida Handwriting, available in windows):

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

Download (see blog for more details):

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

Wordcloud:

 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) 
+2
source

In addition to the previous answers and explain how you can actually choose which fonts to use. First, import fonts (you can set a path other than the default in font_import()

 library(extrafont) font_import(prompt = FALSE) 

To find out which fonts are available:

 unique(fonttable()$FamilyName) 

Here is an exact link to what to include as a "font family." Then you can issue the wordlcoud command as follows:

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

Why font = 1 ? From ?par() , here what he says about the font parameter:

An integer that indicates which font to use for the text. If possible, device drivers are arranged so that 1 corresponds to plain text (default), from 2 to bold, 3 - italics, 4 - bold italics.

0
source

Source: https://habr.com/ru/post/1495776/


All Articles