Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot font family change between versions

In the following picture you can see the same part of a ggplot graph, created in two different (windows) machines. Above each graph I have written the versions of the related packages. I am not using any font family setting in the ggplot call. Why do I get different fonts with the most recent version? (The change reminds me of the effect that Cleartype setting has in smoothing font edges)

picture

like image 654
George Dontas Avatar asked Jan 03 '13 09:01

George Dontas


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


1 Answers

You might want to take a look at this page, http://wiki.stdout.org/rcookbook/Graphs/Fonts/, for some tips on dealing with font issues with ggplot/ggplot2. Also there is an example R script that will generate a table of all the fonts rendered so you can compare them a little easier between the 2 systems.

make_font_table.R

fonttable <- read.table(header=TRUE, sep=",", stringsAsFactors=FALSE,
                        text='
Short,Canonical,
mono,Courier,
sans,Helvetica,
serif,Times
,AvantGarde
,Bookman
,Helvetica-Narrow
,NewCenturySchoolbook
,Palatino
,URWGothic
,URWBookman
,NimbusMon
URWHelvetica,NimbusSan
,NimbusSanCond
,CenturySch
,URWPalladio
URWTimes,NimbusRom
')

fonttable$pos <- 1:nrow(fonttable)

library(reshape2)
fonttable <- melt(fonttable, id.vars="pos", measure.vars=c("Short","Canonical"),
                  variable.name="NameType", value.name="Font")

# Make a table of faces. Make sure factors are ordered correctly
facetable <- data.frame(Face = factor(c("plain","bold","italic","bold.italic"),
                                      levels = c("plain","bold","italic","bold.italic")))

fullfonts <- merge(fonttable, facetable)

library(ggplot2)
pf <- ggplot(fullfonts, aes(x=NameType, y=pos)) + 
             geom_text(aes(label=Font, family=Font, fontface=Face)) +
             facet_wrap(~ Face, ncol=2)

pf

You can run it like so:

% R
> source ("make_font_table.R")
> pf

   ss of font table

NOTICE: Only some of the fonts (Timea, Helvetica, Courier) are actually getting rendered.

Also you might want to check out the extrafont-package. Finally this post shows how to use the extrafont-package so that you get better looking fonts rendered in your output.

like image 156
slm Avatar answered Oct 04 '22 12:10

slm