Add font to R that is not in extrafonts library
First you get the font you want and install it on your system. Nothing to do with R. Test if the font works by checking in any regular program like MS Word or something.
Then open R, load extrafont
package and import the font that you installed. I think it only works with .ttf
fonts for now.
library(extrafont)
font_import(pattern="Roboto")
If this works, then this step will add those fonts to the extrafontdb. You will see something like this...
> font_import(pattern="Roboto",prompt=FALSE)
Scanning ttf files in C:\windows\Fonts ...
Extracting .afm files from .ttf files...
C:\Windows\Fonts\Roboto-Black.ttf => C:/R/R-3.5.1/library/extrafontdb/metrics/Roboto-Black
C:\Windows\Fonts\Roboto-BlackItalic.ttf => C:/R/R-3.5.1/library/extrafontdb/metrics/Roboto-BlackItalic
...
C:\Windows\Fonts\RobotoCondensed-Regular.ttf => C:/R/R-3.5.1/library/extrafontdb/metrics/RobotoCondensed-Regular
Found FontName for 30 fonts.
Scanning afm files in C:/R/R-3.5.1/library/extrafontdb/metrics
Writing font table in C:/R/R-3.5.1/library/extrafontdb/fontmap/fonttable.csv
Writing Fontmap to C:/R/R-3.5.1/library/extrafontdb/fontmap/Fontmap...
This is a one time thing. Once imported, it is available within R from then on. All you have to do is run below.
library(extrafont)
# for windows
windowsFonts(sans="Roboto")
loadfonts(device="win")
loadfonts(device="postscript")
Now the defaults should have changed.
plot(x=1:5,y=1:5)
ggplot
has base_family
which needs to be changed and family
argument for text geoms.
library(ggplot2)
p <- ggplot(data.frame(x=1:5,y=1:5),aes(x,y))+
geom_point()+
geom_text(aes(label=y),nudge_x=0.5,family="Roboto")+
theme_bw(base_family="Roboto")
p
Exporting raster images should work too.
ggsave("plot.png",p)
PDFs are a pain. They have an extra family
argument. There is also something about embedding and stuff. See link below.
ggsave("plot.pdf",p,family="Roboto")
All the info you need is here.
Adding Fonts to R using extrafont or showtext libraries (on Mac via FontBook)
In Font Book > Preferences, by switching the preference of the fonts from User to Computer, it ensures that the fonts install into the fonts folder. This solved my problem.
Cannot import fonts into R
The best solution to my knowledge is showtext
and sysfonts
. First, add the font to the session:
# directly from google fonts
sysfonts::font_add_google("Roboto Condensed")
# or add an arbitrary font with
sysfonts::font_add("Roboto Condensed", regular = "RobotoCondensed-Regular.ttf")
Once this is done, simply load showtext
and run showtext_auto()
once to activate it (you need to repeat add_font*
and showtext_auto
every session):
library(ggplot2)
library(showtext)
showtext_auto()
ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
labs(x="Fuel efficiency (mpg)", y="Weight (tons)",
title="Seminal ggplot2 scatterplot example",
subtitle="A plot that is only useful for demonstration purposes",
caption="Brought to you by the letter 'g'") +
hrbrthemes::theme_ipsum_rc()
Created on 2022-03-22 by the reprex package (v2.0.1)
You can also try to make the font available permanently. But it seems to be hit and miss. Theoretically, you need to install the font into C:\Windows\Fonts
, which you can do by 1) unzip the fonts, 2) right click and "install for all users". If you install the fonts in a different way, there is a good chance Windows will only put a link into C:\Windows\Fonts
, which R can't deal with.
You can check available fonts with:
fonts_df <- sysfonts::font_files()
View(fonts_df)
This works for most fonts in my experience but with Roboto, I still had no luck for some reason. add_font*
seems to be the best way to go for now.
How can I resolve the No Font Name issue when importing fonts into R using extrafont?
As it was mentioned by @Moritz Schwarz, the problem is traced to Rttf2pt1
.
According to a solution proposed here, downgrading it to 1.3.8 will fix the problem:
library(extrafont)
library(remotes)
remotes::install_version("Rttf2pt1", version = "1.3.8")
extrafont::font_import()
Downloading Fonts in R: 'No fontName. Skipping'
Using the following code worked when I downloaded Rtools40.
library(extrafont)
library(remotes)
remotes::install_version("Rttf2pt1", version = "1.3.8")
extrafont::font_import()
Verify font availability in R
You're already using the extrafont
package so you can use fonts()
to see the registered fonts. To check if a particular font is available you can do:
library(extrafont)
"metropolis" %in% fonts()
[1] FALSE
How to solve the error status 5 in extrafont?
I suspect this is related to issues with Rttf2pt1 (upon which extrafont depends) as described here and here. Suggested solution which worked for me is to downgrade Rttf2pt1 as below:
remove.packages("Rttf2pt1")
remotes::install_version("Rttf2pt1", version = "1.3.8")
Importing the xkcd fonts in R (for xkcd package) on windows
Here is the solution I reached.
Download necessary packages.
install.packages("extrafont","remotes","xkcd")
library(extrafont)
library(remotes)
One of the problem is the error message In system2(ttf2pt1, c(args, shQuote(ttfiles[i]), shQuote(tmpfiles[i])), : running command
[...] that is resolved by downgrading Rttf2pt1
to version 1.3.8
remotes::install_version("Rttf2pt1", version = "1.3.8")
Download the font and be sure it is in the working directory (it should be).
download.file("http://simonsoftware.se/other/xkcd.ttf", dest = "xkcd.ttf", mode = "wb")
The original description to import the font does not work (see the vignette("xkcd-intro"
), the path has to be redirected to the working directory.
font_import(paths = getwd(), pattern = "[X/x]kcd", prompt = FALSE)
Loading the font and the xkcd package should now work.
loadfonts(device = "win")
library(xkcd)
The package xkcd and its functions should not work properly.
Related Topics
Plot Margins in Rmarkdown/Knitr
Re- Installing R Linux Ubuntu: Unmet Dependencies R
Fitting a Lognormal Distribution to Truncated Data in R
How to Merge Multiple Data.Frames and Sum and Average Columns at the Same Time in R
Ggplot Dotplot: What Is the Proper Use of Geom_Dotplot
Prevent Knitr/Rmarkdown from Interleaving Chunk Output with Code
Automatically Detect Date Columns When Reading a File into a Data.Frame
How to Convert a Factor Column That Contains Decimal Numbers to Numeric
How to Use "Cast" in Reshape Without Aggregation
R Equivalent of Stata Local or Global MACros
How to Calculate the Median on Grouped Dataset
How to Change Factor Labels into String in a Data Frame
Regression Line for the Entire Data Set Together with Regression Lines Based on Groups
How to Use the Function Curve in [R] to Graph a Normal Curve
How to Use a Non-Ascii Symbol (E.G. £) in an R Package Function