I'm trying to make a square pie with waffle function but the male icon can't be used
library(waffle)
library(extrafont)
parts <- c(`Sick=14` =14, `Treated=19` = 19, `Not sick=7` =7)
loadfonts(device = "win")
system.file("fonts", package="waffle")
waffle(
parts, rows = 5, colors = c("#FD6F6F", "#93FB98", "#D5D9DD"),
use_glyph = "male", size = 8 ,title = 'convenient title'
)
Thank you .
This is most likely to a funky installation of Font Awesome. There are many version with various names around. Check that you have "FontAwesome" installed with the following command:
> fonts()[grep("Awesome", fonts())]
[1] "FontAwesome"
then you should be able to get this:
What worked for me was following all these instructions (https://www.listendata.com/2019/06/create-infographics-with-r.html) BUT installing the FontAwesome package 4.7 that you can download here (https://fontawesome.com/versions). Otherwise I think it seems newer packages have a different font/family name (Free Reegular version whatever) that waffle can not read.
I would say main points are:
Add FontAwesome 4.7. font (.ttf) to your computer clicking on the .ttf file in the package once unzipped.
Make sure you remove other versions of FontAwesome from your computer and from the fonttable of extrafont (https://stackoverflow.com/a/70386036/4438465)
Make sure you also add the font with the font_add option in showtext package (see step 7 in the first link from listendata).
Related
I'm currently trying to display Korean in my graph (a simple histogram), which I've generated in R.
But it won't display Korean correctly.
Could someone please help? Many thanks in advance!
hist(df$var, main = "abc가나다", xlab = "def마바사")
You are probably using the default font — Helvetica — which is missing Hangul glyphs. You can either configure a font which has these via par(family = …).
Or you can use the ‘ragg’ package to plot to a graphics device which supports font fallback: this means that the graphics device will automatically select a font that supports the glyphs you’re using.
With ‘ragg’, you can do the following:
capture = ragg::agg_capture()
hist(df$var, main = "abc가나다", xlab = "def마바사")
plot_data = capture()
dev.off()
plot(as.raster(plot_data))
Admittedly, this is a bit cumbersome. However, for plotting to a file, using ‘ragg’ is no more effort than using base R: you just replace the device, e.g. agg_png instead of png.
I would like to create a plot in R using dice font for values 1 to 6. Dice font for windows can be downloaded here - copy the downloaded true type font to the C:\Windows\Fonts directory and it can be used in windows applications like Excel or Word.
I found out about the showtext package in this question for adding fonts to R which seems to add the dice font using the R script below.
require(showtext)
font_add(family = "dice",regular = "C:/Users/Mark/AppData/Local/Microsoft/Windows/Fonts/dice.ttf")
For some reason, I don't understand I had to set the path to the AppData/Local directory to get font_add to no report a could-not-find-it error. The next challenge was to get was to make a simple test plot
plot(c(1,2),c(2,1),pch = 0, col = "blue", type = "p", cex =5)
text(c(1,2),c(2,1),c(2,1),col = "red")
However, when I attempt to use the dice font using the text funtion I get an error ...
font family not found in Windows font database
text(c(1,2),c(2,1),c(2,1),col = "red", family = "dice")
Questions:
Is there a way to display the fonts accessible to R so I can determine if the dice font is available?
If dice font is available then how do I get the font working with the text function?
Is there an alternative or better way to approach this idea?
After exploring the example suggested by Andrew Brēza I managed to get a result using ggplot2 - I could not find a solution for base R. With respect to the three questions I listed in the original post
showtext has two functions to list the font families and fonts available to are respectively
font_families()
font_files()
I also found that I needed to install dice font using the windows right-click option 'install for all users' else the font file was installed under a user AppData/Local directory not the C:\Windows\Font directory
2.showtext does not appear to work with the base R text function even though the fonts are listed as available - I therefore explored ggplot
3 ggplot with showtext is the only working solution - script below produces the results I was looking for - I will just have to spend a bit of time making it look like a base R plot
# Plot numbers using dice font
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# https://stackoverflow.com/questions/34522732/changing-fonts-in-ggplot2
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
require(showtext)
# Note install the dice font using right-click install for all users
# Otherwise the font file is install in user profile nto C:\Windows\fonts
# Add the dice font to R
font_add(family = "dice",regular = "C:/Windows/Fonts/dice.ttf")
# Checks to show the dice font available in the fonts available to R
font_families()
font_files()
# Automatically use show text for newplots - do I need this?
showtext_auto()
# Load ggplot2
require(ggplot2)
# Create some data
df <- data.frame(rbind(c(1,2,2),c(2,1,4)))
# Plot the dice values
ggplot(df, aes(x = X1, y = X2)) +
geom_point(size = 0.1, shape = 0) +
annotate("text",df[1,1],df[1,2], label = df[1,3], family = "dice", size = 10) +
annotate("text",df[2,1],df[2,2], label = df[2,3], family = "dice", size = 10)
I am having a problem of getting Fontawesome5 to work in R. I have installed the latest version of R, and therefore all have updates all the packages used as well, and I can't get the font to work in R. It is used to make info graphics, see the website here: https://www.listendata.com/2019/06/create-infographics-with-r.html
Once I upload the ttf. files into R i get this error message:
In grid.Call.graphics(C_text, as.graphicsAnnot(x$label), ... :
font family 'FontAwesome5Free-Solid' not found, will use 'wqy-microhei' instead
This means that I get a plot back with no font in it, just a dot. (see picture) [R plot without font1
It would appear R can't put Fontawesome5(5th version) onto the ggplot2/pdf. Is there a way to make this font work in R with ggplot?, regards James.
library(ggplot2)
library(waffle)
library(extrafont)
library(tidyverse)
library(echarts4r)
library(echarts4r.assets)
install.packages("extrafontdb", repos = "http://cran.rstudio.com/")
library(extrafont)
#font download: https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/fonts/fontawesome-webfont.ttf
extrafont::font_import (path="C:/file/location/Documents/R", pattern = "awesome", prompt = FALSE)
install_fa_fonts()
extrafont::font_import (path="C:/Users/User/Documents/R/win-library/4.0/waffle/fonts") #path is where R installs file:
loadfonts(device = "win") #loads in fonts
fonts()[grep("Awesome", fonts())] # should say Awesome fonts 5 in installed.
waffle(
c(`Poor=10` =10, `Average=18` = 18, `Excellent=7` =7), rows = 5, colors = c("#FD6F6F", "#93FB98", "#D5D9DD"),
use_glyph = "female", glyph_size = 12 ,title = 'Girls Performance', legend_pos="bottom"
)
This happened to me after I had updated my Mac OS to Catalina. After weeks of research (and minutes after I had commented on your post here), I found a solution that at least worked for me.
It seems (but please correct me if I'm wrong) that the R package extrafont can only deal with .ttf font files. And it seems like Catalina converted my fonts to .ttc, so I had to convert these back into .ttf and copy paste these .ttf into the System fonts folder.
In short, convert your fonts from .ttc to .ttf
Similar to what they figured out here:
https://github.com/wch/extrafont/issues/32
I used transfonter.org to convert my fonts.
I am building a map in R that I would like to have the text to be shown in Linux Libertine font. Package extrafont is loaded, fonts have been loaded and the path to ghostscript is correctly set.
When I use the following command, R saves the output and everything works fine.
ggsave(file = foo.eps, plot = map, width = 15, height = 10, units = "cm", family='Linux Libertine Display')
However when I instead use family='Linux Libertine', I receive the following error message:
Error in grDevices::postscript(..., onefile = FALSE, horizontal = FALSE, :
unknown family 'Linux Libertine'
It seems that it can't find the font, which is weird as it is listed in the return of fonttable(). Any ideas how I can make R to use the font?
The link provided by user TomNash does indeed explain the problem and the solution:
The problem is that some fonts (and this includes Linux Libertine) have distinct font face names (Linux Libertine Bold, Linux Libertine Italics, etc.) but all share the same family name (Linux Libertine). The extrafont package cannot distinguish between those fonts, because it only looks at the family name (and in the above example Linux Libertine Display works, because this is a unique family name).
The easiest way to fix this is to locate the directory of the font table: system.file("fontmap", "fonttable.csv", package="extrafontdb") and then open the fonttable.csv and copy for all Linux Libertine fonts (or whatever fonts this concerns) the font name into the font family cell. Then return to R and execute loadfonts() again to make sure R rebuilds the font table.
I've installed the 'extrafont' package in order to install an external font library Duality via the ttf_import() method. However, when specifying the font via the wordcloud method, I receive 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]
In order to verify that the font is indeed installed, I issued the following commands
> choose_font("Duality")
[1] "Duality"
> fonts()
....[49] "Waree" "Duality"
How come the Duality font is not visible to the vfont parameter of wordcloud? And how do I make it visible to Cairo (the default renderer). TIA!
I've been able to overcome the same problem using the parameters passed to text family and font and described in ?par instead of vfont. Also I needed to load the font first. So the thing goes:
Import the font (sorry, the link to Duality provided in OP is no longer available, I use Lucida Handwriting instead, available in windows):
library(extrafont)
font_import(pattern="LHANDW")
Load (see this blog for 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)
To complement previous answers, and explain how one can actually choose which fonts to use. First, import fonts (it is possible to set a path different from the default in font_import()
library(extrafont)
font_import(prompt = FALSE)
To know which fonts are available:
unique(fonttable()$FamilyName)
This presents the exact reference for what to include as "font family". You can then issue the wordcloud command like this:
wordcloud(c(letters, LETTERS, 0:9), seq(1, 1000, len = 62), family = "Carlito", font = 1)
Why font = 1? From ?par(), here's what it says about the font parameter:
An integer which specifies which font to use for text. If possible,
device drivers arrange so that 1 corresponds to plain text (the
default), 2 to bold face, 3 to italic and 4 to bold italic.