Re: [R] ggplot2: How to change font of labels in geom_text

14,291 views
Skip to first unread message

Dennis Murphy

unread,
Dec 31, 2009, 4:09:47 PM12/31/09
to Ronan Reilly, ggplot2
Hi:

I've moved this discussion to the ggplot2 list, where it more appropriately belongs.

I tried a couple of things without success, but first, so we understand the frame of reference...

p <- ggplot(small.df, aes(x=row, y=col, label=item, fill=latency))
p <- p + geom_tile()

where small.df is the data.frame in the original post below.

(1) added a theme_text line as follows:

p + opts(theme_text(family = 'courier'))

No visible difference in the plot - fonts still look Arial/Helvetica to me.

(2) OK. Maybe it's the geom_text preceding the theme_text...can I incorporate
     family = 'courier' into geom_text?

p + geom_text(colour = 'white', family = 'courier')

No error, plot renders, still Helvetica.

(3) Precede geom_text with theme_text..(getting desperate...)

p + opts(theme_text(family = 'courier')) + geom_text(colour = 'white')

Same as previous two tries (plus the original).

Is courier an 'acceptable' font family for ggplot()?

My settings:
> sessionInfo()
R version 2.10.1 (2009-12-14)
i386-pc-mingw32

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252  
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                         
[5] LC_TIME=English_United States.1252   

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods 
[8] base    

other attached packages:
[1] ggplot2_0.8.5 digest_0.4.2  reshape_0.8.3 plyr_0.1.9    proto_0.3-8 

loaded via a namespace (and not attached):
[1] tools_2.10.1

Dennis




On Thu, Dec 31, 2009 at 12:14 PM, Ronan Reilly <ronan....@gmail.com> wrote:
Happy New Year everyone.

I have what I hope is a simple-to-answer question.  In the code sample
below, I'm using a tile plot to plot a continuous measure and overlaying the
plot with labels.  I need to print the labels in courier, but there appears
to be no way to override the default font setting in geom_text.  I've
scoured the web for clues on how to do this, but found nothing relevant.

Any help would be very much appreciated.

Ronan

---- start code snippet ----

small.df <- data.frame(scan(what=list(row=0, col=0, item="", latency=0)))
1 1 ab 0.1
1 2 bc 0.2
1 3 cd 0.3
2 1 de 0.4
2 2 ef 0.5
2 3 fg 0.6
3 1 gh 0.7
3 2 hi 0.8
3 3 ij 0.9

small.df

p <- ggplot(small.df, aes(x=row, y=col, label=item, fill=latency))
p <- p + geom_tile()
p <- p + geom_text(colour="white")
print(p)

---- end code snippet ----

--
Professor Ronan Reilly
Department of Computer Science
NUI Maynooth
Maynooth
Co. Kildare
IRELAND

t: +353-1-7083846
e: Ronan....@nuim.ie
w: http://www.cs.nuim.ie; http://cortex.cs.nuim.ie

______________________________________________
R-h...@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

hadley wickham

unread,
Jan 2, 2010, 5:01:04 PM1/2/10
to Dennis Murphy, Ronan Reilly, ggplot2
It doesn't work because I've never implemented this functionality.
You can track progress at
http://github.com/hadley/ggplot2/issues/#issue/60.

The syntax would be

p + geom_text(colour = 'white', fontfamily = 'courier')

And see the following for non-standard font faces:

@article{Rnews:Murrell+Ripley:2006,
author = {Paul Murrell and Brian Ripley},
title = {Non-Standard Fonts in {PostScript} and {PDF} graphics},
journal = {R News},
year = 2006,
volume = 6,
number = 2,
pages = {41--47},
month = {May},
url = {http://CRAN.R-project.org/doc/Rnews/},
pdf = {http://CRAN.R-project.org/doc/Rnews/Rnews_2006-2.pdf}
}

Hadley

> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> To post to this group, send email to ggp...@googlegroups.com
> To unsubscribe from this group, send email to
> ggplot2+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/ggplot2

--
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages