Mike Wise
Mike Wise

Reputation: 22847

Change default font in ggplot2

So this is a series of questions really. I would like to change the default font in ggplot2, and I understand the easiest way is to change the default family in theme_gray.

But I am puzzling how to do that. I can do this:

> theme_set(theme_gray(base_size = 18))

But I can't do this:

> theme_set(theme_gray(family="mono"))
Error in theme_gray(family = "mono") : unused argument (family = "mono")

And then when I look at the help, I wonder if I shouldn't be using theme_update as I am only changing one member. And then I see that it also mentions under "See also":

%+replace% and +.gg

And I wonder if I should be using those instead. Of course I could not get them to work either...

So what works? And more importantly, what should one be using to keep up in the fast changing ggplot2 world?

Upvotes: 3

Views: 6339

Answers (2)

Gopala
Gopala

Reputation: 10483

You can get many fonts with the package extrafont and use it as follows:

library(extrafont)
p + theme(title = element_text(family = 'Helvetica')) # whatever font you want

Upvotes: 4

r.bot
r.bot

Reputation: 5424

The documentation have now been updated. You can use base_family to set the font.

p <- ggplot(mtcars) + geom_point(aes(x = wt, y = mpg,
                                     colour=factor(gear))) + facet_wrap(~am)

p + theme_gray(base_family = "mono")

Upvotes: 4

Related Questions