Changing Fonts in Ggplot2

Changing fonts in ggplot2

You just missed an initialization step I think.

You can see what fonts you have available with the command windowsFonts(). For example mine looks like this when I started looking at this:

> windowsFonts()
$serif
[1] "TT Times New Roman"

$sans
[1] "TT Arial"

$mono
[1] "TT Courier New"

After intalling the package extraFont and running font_import like this (it took like 5 minutes):

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

I had many more available - arguable too many, certainly too many to list here.

Then I tried your code:

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)

yielding this:

Sample Image

Update:

You can find the name of a font you need for the family parameter of element_text with the following code snippet:

> 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)

yields:
Sample Image

Modifying fonts in ggplot2

Sorted out my query with fairly minimal hassle. It was a two-step solution that I wouldn't have arrived at without following the advice of the members who responded.

To change the ggplot text defaults, I adapted the code that Brandon referred me to at:

http://johndunavent.com/combined-line-and-bar-chart-ggplot2

Where John Dunavent creates a function, theme_min, that can be edited to provide the default options for a ggplot, including using fonts imported from Windows with the windowsFonts command. My adaptation of his code looks like this:

theme_min = function (size=10, font=NA, face='plain', 
panelColor=backgroundColor, axisColor='#999999',
gridColor=gridLinesColor, textColor='black')
{
theme_text = function(...)
ggplot2::theme_text(family=font, face=face, colour=textColor,
size=size, ...)

opts(
axis.text.x = theme_text(),
axis.text.y = theme_text(),
axis.line = theme_blank(),
axis.ticks = theme_segment(colour=axisColor, size=0.25),
panel.border = theme_rect(colour=backgroundColor),
legend.background = theme_blank(),
legend.key = theme_blank(),
legend.key.size = unit(1.5, 'lines'),
legend.text = theme_text(hjust=0),
legend.title = theme_text(hjust=0),
panel.background = theme_rect(fill=panelColor, colour=NA),
panel.grid.major = theme_line(colour=gridColor, size=0.33),
panel.grid.minor = theme_blank(),
strip.background = theme_rect(fill=NA, colour=NA),
strip.text.x = theme_text(hjust=0),
strip.text.y = theme_text(angle=-90),
plot.title = theme_text(hjust=0),
plot.margin = unit(c(0.1, 0.1, 0.1, 0.1), 'lines'))
}

##Create a custom font type. Could be 'F', 'TEST', whatever
windowsFonts(F = windowsFont('Wide Latin'))

##and insert this line of code into the original code I list above:
+ theme_min(font='F', size=10)

Awkwardly, there is no way (that I found) to generically modify the font settings for geom_text objects before a plot is created. James' solution above worked perfectly for this, though. Instead of using a standard font, I set fontfamily="F" to bring in the custom font that I selected in theme_min(), i.e.:

grid.gedit("GRID.text",gp=gpar(fontfamily="F"))

Hopefully this is useful to any other users looking to modify fonts on their graphs.

Cheers to all who helped me sort this out!
Aaron

Change font in ggplot

Solved: Key point is to run: font_import() followed by loadfonts(device="win")

Changing Fonts for Graphs in R

You can use the extrafont package.

install.packages("extrafont")
library(extrafont)
font_import()
loadfonts(device="win") #Register fonts for Windows bitmap output
fonts() #vector of font family names
## [1] "Andale Mono" "AppleMyungjo"
## [3] "Arial Black" "Arial"
## [5] "Arial Narrow" "Arial Rounded MT Bold"

library(ggplot2)
data(mtcars)
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() +
ggtitle("Fuel Efficiency of 32 Cars") +
xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") +
theme_bw() +
theme(text=element_text(family="Times New Roman", face="bold", size=12)) #Times New Roman, 12pt, Bold
#example taken from the Github project page

Sample Image

Note: Using the extrafont package, you can also embed these fonts in PDF and EPS files (make plots in R and export to PDF/EPS). You can also directly create math symbols (see math equation in plot below), usually created using TeX. More information here and here. Also look at the github project page.

Sample Image

Also look at this answer which describes creating xkcd style graphs using the extrafont package.

Sample Image

Change Font of ggplot Title and Data Label

You can run in the theme command, text = element_text(family = "Comic Sans MS") to change the font. You can run this code:

 mydf %>%
gather(Month, Total, -Category) %>%
mutate(Month = reorder(Month, row_number())) %>%
mutate(Category = reorder(Category, row_number())) %>%
ggplot(aes(Month, Total, fill = Category, group = Category)) +
geom_text(aes(label=Total), position=position_dodge(width=0.9), vjust=-0.25) +
scale_fill_manual(values = c("dark green", "red")) +
geom_bar(stat = "identity", position = "dodge") +
labs(x = "", y = "", title = "Title of Plot", subtitle = "Subtitle of Plot") +
theme_bw() +
theme(legend.position = "bottom",
legend.title = element_blank(),
legend.key.size = unit(0.5, 'cm'),
legend.text = element_text(size=7),
panel.grid.major.x = element_blank(),
panel.border = element_blank(),
plot.title = element_text(hjust = 0),
plot.subtitle = element_text(size=8, hjust=0, face="italic", color="black"),
axis.text.x = element_text(size = 10, face = "bold", color="black")) +
theme(text = element_text(family = "Comic Sans MS"))

Output:

Sample Image



Related Topics



Leave a reply



Submit