How to change the default font in ggplot 2
1
0
Entering edit mode
8 weeks ago
camillab. ▴ 50

Hi!

I want to change the font of my scatterplot created with ggplot2 and use Arial as font. I did first check the default font so I did:

windowsFonts()


And I get:

$serif [1] "TT Times New Roman"$sans
[1] "TT Arial"

\$mono
[1] "TT Courier New"


So this means that the default font it Times New roman? if so, how do I change it with Arial? If I understood correctly, I should use extrafont package but when I try to import the fonts it doesn't work (it gives me No FontName. Skipping error). here my code:

install.packages("extrafont")
library(extrafont)
font_import()


So I did try use an older version of Rttf2pt1 :

library(remotes)
remotes::install_version("Rttf2pt1", version = "1.3.8")


and it loaded OK but when I try to import the font I get:

Scanning ttf files in C:\WINDOWS\Fonts ...
Extracting .afm files from .ttf files...
Error in ttf_extract(ttfiles) :
lazy-load database 'N:/R/win-library/4.1/Rttf2pt1/R/Rttf2pt1.rdb' is corrupt
In addition: Warning message:
In ttf_extract(ttfiles) : internal error -3 in R_decompress1


so I am not sure what I am doing wrong but I am also not sure this is the best way to do since all the post I've found are pretty old so maybe new fast method/package exist.

Can you help me with this error or do you know if there is a simpler way to change the font in ggplot2?

Thank you!

Camilla

font ggplot2 Arial • 710 views
0
Entering edit mode
8 weeks ago

Hi Camilla,

The first solution that is suggested here works for me (on Windows): https://stackoverflow.com/questions/34522732/changing-fonts-in-ggplot2

That is:

library(extrafont)
font_import() # requires confirmation; runs for ~3 minutes

library(ggplot2)
a <- ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
ggtitle('Fuel Efficiency of 32 Cars') +
xlab('Weight (x1000 lb)') +
ylab('Miles per Gallon') +
theme(text = element_text(size = 16,  family = 'Wingdings'))
a


If font_import() is returning an error for you, can you check C:\Windows\Font\ to see if there are TTF files there?

0
Entering edit mode

I did try your code but my plot doesn't change and when I see the warnings() it gives me font family not found in Windows font database but on C:\Windows\Font\ I have the TTF files there!

I did try fonts() but I got NULL as output (I guess it didn't import any font)

And if I do font_import() I get:

Scanning ttf files in C:\WINDOWS\Fonts ...
Extracting .afm files from .ttf files...
Error in ttf_extract(ttfiles) :
lazy-load database 'N:/R/win-library/4.1/Rttf2pt1/R/Rttf2pt1.rdb' is corrupt
In addition: Warning messages:
1: In ttf_extract(ttfiles) : restarting interrupted promise evaluation
2: In ttf_extract(ttfiles) : internal error -3 in R_decompress1


Do you think it could be because of the old version of Rttf2pt1 since the message error is the same?

0
Entering edit mode

What happens if you just try:

BiocManager::install('Rttf2pt1')

0
Entering edit mode

I still get the same error message saying that the font family not found in Windows font database but I dont understand why

0
Entering edit mode

Perhaps force delete this directory:

N:/R/win-library/4.1/Rttf2pt1

0
Entering edit mode

Apologies for the late reply but I did try few different things like deleting the folder as you suggested, delete all the packages and re-install them and update others package just in case there was a dependency but I still get the error messages when I do font_import(). I got first a pop up message saying

ttf2p1.exe has stopped working

and it does for every font, not for one specific and on R studio:

Scanning ttf files in C:\WINDOWS/Fonts, C:\Users\sjjgcbo\AppData\Local/Microsoft/Windows/Fonts ...
Extracting .afm files from .ttf files...
C:\Windows\Fonts\AGENCYB.TTF : No FontName. Skipping.
`