Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying fonts in ggplot2

Tags:

r

ggplot2

I am looking for a way to modify font types in ggplot. At the moment I would be happy enough to simply change fonts to the 'courier' font family, but ultimately my goal is to call a custom font template--any input on this latter point would be very much appreciated.

I've done a bit of homework, looking at the following posts and articles:

  • ggplot2: How to change font of labels in geom_text
  • R News Volume 6/2, Non-Standard Fonts in PostScript and PDF Graphic, Murrell and Ripley.
  • ComputerModern font in ggplot2 graphics?

It may be because I am still a hopeless amateur with ggplot2, but I haven't even been able to switch chart fonts to courier. Any help? I've included the data for the chart in question, below, along with the code, so hopefully this is all easy enough to follow.

like image 409
aaron Avatar asked Nov 04 '10 05:11

aaron


People also ask

Can you change font in ggplot2?

ggplot allows you to change the font of each part of the figure: you just need to know the correct option to modify in the theme. (For a full list of customizable components of the theme, see this documentation.)

What font does ggplot2 use?

Using Inkscape, the default font for all my ggplot2 plots is Arial.


Video Answer


3 Answers

I think your answer is fine but you can do it more simply:

install.packages("extrafont");library(extrafont) font_import("Trebuchet MS") library(ggplot2) qplot(1:10)+theme(text=element_text(family="Trebuchet MS")) 
like image 73
Steve Powell Avatar answered Sep 19 '22 08:09

Steve Powell


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

like image 26
aaron Avatar answered Sep 20 '22 08:09

aaron


Have a look at the family argument of theme_text()

dummy <- data.frame(A = rnorm(10), B = rnorm(10))
ggplot(dummy, aes(x = A, y = B)) + geom_point()
#helvetica = default
ggplot(dummy, aes(x = A, y = B)) + geom_point() + opts(axis.title.x = theme_text(family = "sans", face = "bold"))
#times
ggplot(dummy, aes(x = A, y = B)) + geom_point() + opts(axis.title.x = theme_text(family = "serif", face = "bold"))
#courier 
ggplot(dummy, aes(x = A, y = B)) + geom_point() + opts(axis.title.x = theme_text(family = "mono", face = "bold"))
like image 24
Thierry Avatar answered Sep 22 '22 08:09

Thierry