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 in Others by Soham
• 8,730 points
354 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 by Rahul
• 8,980 points

Related Questions In Others

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
249 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
798 views
0 votes
1 answer

Events & Threading in Windows Forms

I don't think you should be updating ...READ MORE

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

Using/Handling colon inside a JSF generated HTML element ID in CSS selector

Yes, you can.  Just Backslash (\) the colon.  Like ...READ MORE

answered Nov 14, 2018 in Others by DataKing99
• 8,240 points
1,660 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 7,714 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
20,736 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,010 points
14,932 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
639 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 in Others by Rahul
• 8,980 points
34 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 in Others by Rahul
• 8,980 points
32 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP