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,700 points
5,770 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,670 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 ...READ MORE

answered Nov 24, 2022 in Others by narikkadan
• 63,420 points
320 views
0 votes
1 answer

Two different fonts in one cell using excel vba

Public Sub add_degree_symbol() ActiveCell.Value = ...READ MORE

answered Apr 10, 2023 in Others by Kithuzzz
• 38,010 points
592 views
0 votes
1 answer

Reducing sequences in an array of strings

I've written a C# app to solves ...READ MORE

answered Nov 2, 2018 in Others by DataKing99
• 8,240 points
730 views
0 votes
1 answer

Avoiding memory leaks in C++

If you use smart pointers at all ...READ MORE

answered Nov 5, 2018 in Others by nirvana
• 3,130 points
1,195 views
0 votes
1 answer

How to plot side-by-side Plots with ggplot2 in R?

By Using gridExtra library we can easily ...READ MORE

answered Apr 16, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 9, 2020 by MD 8,532 views
+1 vote
1 answer

How to change fill color in each facet using ggplot2?

You can map the facetting variable to ...READ MORE

answered May 8, 2018 in Data Analytics by kappa3010
• 2,090 points
24,728 views
0 votes
11 answers

Changing the legend title in ggplot

Hi, you can also try guides() to ...READ MORE

answered Jul 30, 2019 in Data Analytics by Cherukuri
• 33,030 points
16,988 views
0 votes
1 answer

How to order bars in a bar graph using ggplot2?

The key to ordering is to set ...READ MORE

answered Jun 1, 2018 in Data Analytics by DataKing99
• 8,240 points
916 views
0 votes
1 answer

can somebody explain me what does "passing by value" and "Passing by reference" mean in C#?

To answer your question, “passing by value” ...READ MORE

answered Feb 10, 2022 in Others by Rahul
• 9,670 points
601 views
0 votes
1 answer

Easiest way to convert int to string in C++

The C++11 version has introduced the std::stoi ...READ MORE

answered Feb 10, 2022 in Others by Rahul
• 9,670 points
325 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP