Changing fonts in ggplot2

0 votes

Previously, I was able to make alterations to the ggplot2 font by using the code mentioned below:-
windowsFonts(Times=windowsFont("TT Times New Roman")).

But regardless of this, I am now not able to get it off this due to the reason that while trying to set the family="" in ggplot2 theme(), the change of fonts generation is not successful. I am trying to compile the MWE shown below with various  font families.

library(ggplot2) library(extrafont) loadfonts(device = "win") 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="Comic Sans MS")) # family="CM Roman")) # family="TT Times New Roman")) # family="Sans")) family="Serif")) print(a) print("Graph should have refreshed")

I observed that when the R returns a warning, if the Windows font database does not show the font family as results, then this is a normal step and is not a problem. Due to the graph using an arial type font, enabled it to work at one time Therefore, this has always been a present warning even during the initial times migration.

UPDATE

when I run windowsFonts() my output is

$serif [1] "TT Times New Roman"

$sans [1] "TT Arial"

$mono [1] "TT Courier New"

If this is after you have run font_import() then I can only conclude that your fonts are not being saved in the right place. The code that ran the font_import() request actually loads the libraries with:

LocalLibraryLocation <- paste0("C:\\Users\\",Sys.getenv("USERNAME"),"\\Documents","\\R\\win-library\\3.2"); .libPaths(c(LocalLibraryLocation, .libPaths()))

Feb 8, 2022 in Others by Soham
• 9,730 points
8,415 views

1 answer to this question.

0 votes

By using the windowsFonts() a user can see what fonts are available with the command .

For example:-

> windowsFonts() 
$serif [1] "TT Times New Roman"
$sans [1] "TT Arial" 
$mono [1] "TT Courier New"

Once the installing process commenced,  the package extraFont and running font_import would work like this:

library(extrafont) 
font_import() 
loadfonts(device = "win")

By trying your code, I got this:-
 

library(ggplot2) 
library(extrafont) 
loadfonts(device = "win") 
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="Comic Sans MS")) 
print(a)

In order to find the font name needed for the family parameter of the element_text, the following code can be used:-

> names(wf[wf=="TT Times New Roman"])
[1] "serif"

And then:

library(ggplot2) 
library(extrafont) 
loadfonts(device = "win")

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="serif")) 
print(a)

answered Feb 8, 2022 by Rahul
• 9,690 points

Related Questions In Others

0 votes
1 answer

How to keep one variable constant with other one changing with row in excel

Use this form: =(B0+4)/$A$0 Since you are dragging across ...