Legend title on 2 lines using plotmath expression

219 views
Skip to first unread message

Dave Auty

unread,
Oct 17, 2010, 3:38:18 AM10/17/10
to ggplot2
Apologies, the control parameter was missing from my original post:

ggplot(cont, aes(Dist, Ht, fill = SG)) + geom_tile() +
xlab("Radius (cm)") + ylab("Height in stem (m)") +
opts(title = "Wood density") +
scale_fill_gradientn(expression(bold(paste("Density
\n(",kg~m^-3,")"))), limits = c(300, 675), colours =
terrain.colors(7))

I think it is some incompatibility with \n that inserts a space before
the units.

Dave

Dave Auty

unread,
Oct 17, 2010, 3:19:37 AM10/17/10
to ggplot2
Hello,

I'm new to ggplot and scoured the archives with no luck. I would like
to to put a legend title for a contour plot on 2 lines as follows:

ggplot(cont, aes(Dist, Ht, fill = SG)) + geom_tile() +
xlab("Radius (cm)") + ylab("Height in stem (m)") +
opts(title = "Wood density") +
scale_fill_gradientn(expression(bold(paste("Density
(",kg~m^-3,")"))), limits = c(300, 675), colours = terrain.colors(7))

However, this only partially works because even though the units are
on a separate line a space is inserted after the opening bracket like
so: ( kg m-3).

I read on one post that control parameters such as \n were
incompatible with plotmath expressions, but I can't find a way to get
around this.

I would also like to set an xlim argument as well but can't see how
this is done.

Thanks in advance for any help.

Dave

> sessionInfo()
R version 2.11.1 (2010-05-31)
i386-pc-mingw32

locale:
[1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United
Kingdom.1252 LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C LC_TIME=English_United
Kingdom.1252

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

other attached packages:
[1] ggplot2_0.8.8 proto_0.3-8 reshape_0.8.3 plyr_1.2.1
lattice_0.19-13 svSocket_0.9-50 TinnR_1.0.3 R2HTML_2.2
Hmisc_3.8-3
[10] survival_2.35-8

loaded via a namespace (and not attached):
[1] cluster_1.13.1 digest_0.4.2 RColorBrewer_1.0-2
svMisc_0.9-60 tools_2.11.1

Luciano Selzer

unread,
Oct 18, 2010, 6:02:37 PM10/18/10
to Dave Auty, ggplot2
Hi Dave I had the same problem. /n and expression don't mix up well. Try to change it outside ggplot. Save as ps and edit it with inkscape for example.


HTH

Luciano


2010/10/17 Dave Auty <dave...@gmail.com>

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Reply all
Reply to author
Forward
0 new messages