The book R graphics provides an example of automatic splitting of a
long paragraph to fit in a rectangle of fixed width. It should be
quite easy to adapt it into a new geom for ggplot2.
library(RGraphics)
grid.draw(splitTextGrob("asdf asdf asdf adsf asdf adsf adsf asdf asdf
adsf asdf adsf asdf asdf asdfasdfasdf asdfasdfasd fadsfads fadsfads
fasdf adsasdf adsf asdfasdfasd fasdfasd fasdfasdfasd fasdfasdfasd
fasdfasdfadsfasd fasdf adsfasdf asdf asdfasdf asdfasdfasdf asdfasdf
asdfasdfa sdfasdfasd fasdfasdfasd fasdfadsfasdf asdfasdf adfs"))
(try resizing the window)
With ggplot2 it would be necessary to specify the width of the
annotation box too.
Let me know if this sounds like a good approach and I'll try to write
the geom later if time permits.
HTH,
baptiste
PS / wish: Would it be possible to restrict the use of a particular
geom to annotate() only with ggplot2? I have a few examples where it
makes no sense at all to define a layer with aesthetic mapping and it
would be nice to throw an error if someone inadvertently tried...
> --
> 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
You could define your own title (or subtitle for that matter),
splitString <- function (text, width)
{
if(is.null(text)) return(NULL)
strings <- strsplit(text, " ")[[1]]
newstring <- strings[1]
linewidth <- stringWidth(newstring)
gapwidth <- stringWidth(" ")
availwidth <- convertWidth(width, "inches", valueOnly = TRUE)
for (i in 2:length(strings)) {
width <- stringWidth(strings[i])
if (convertWidth(linewidth + gapwidth + width, "inches",
valueOnly = TRUE) < availwidth) {
sep <- " "
linewidth <- linewidth + gapwidth + width
}
else {
sep <- "\n"
linewidth <- width
}
newstring <- paste(newstring, strings[i], sep = sep)
}
newstring
}
my.title <- function(label, ...)
textGrob(label=splitString("asdf asdf asdf adsf asdf adsf adsf asdf asdf
adsf asdf adsf asdf asdf asdfasdfasdf asdfasdfasd fadsfads fadsfads
fasdf adsasdf adsf asdfasdfasd fasdfasd fasdfasdfasd fasdfasdfasd
fasdfasdfadsfasd fasdf adsfasdf asdf asdfasdf asdfasdfasdf asdfasdf
asdfasdfa sdfasdfasd fasdfasdfasd fasdfadsfasdf asdfasdf adfs",
width=unit(10, "cm")), ...)
qplot(1:10, 1:10) + opts(plot.title = my.title)
HTH,
baptiste
I think opts(plot.title) expects a function that returns a grob, not
directly a grob. A cleaner version of what I proposed last night might
be,
splitString <- function (text, width)
{
if(is.null(text)) return(NULL)
strings <- strsplit(text, " ")[[1]]
newstring <- strings[1]
linewidth <- stringWidth(newstring)
gapwidth <- stringWidth(" ")
availwidth <- convertWidth(width, "inches", valueOnly = TRUE)
for (i in 2:length(strings)) {
width <- stringWidth(strings[i])
if (convertWidth(linewidth + gapwidth + width, "inches",
valueOnly = TRUE) < availwidth) {
sep <- " "
linewidth <- linewidth + gapwidth + width
}
else {
sep <- "\n"
linewidth <- width
}
newstring <- paste(newstring, strings[i], sep = sep)
}
newstring
}
title <- "Marker1 : Similar to AAA80501.1 Lycopersicon esculentum;
unknown; Solanum lycopersicum unknown oxidase-like protein mRNA,
complete cds; similar to hyoscyamine 6beta-hydroxylase: SwissProt
Accession Number P24397, flavavone-3-hydroxylase: PIR Accession Number
S36233, and ethylene forming enzyme: encoded by GenBank Accession
number M90294 ;cl209ct403cn544 ;COMPLETE ;RESISTANCE(6), FLOWER(71),
FRUIT(7)"
theme_title <-
function (width=unit(1, "npc"), family = "", face = "plain", colour =
"black", size = 10,
hjust = 0.5, vjust = 0.5, angle = 0, lineheight = 1.1)
{
vj <- vjust
hj <- hjust
structure(function(label, x = hj, y = vj, ..., vjust = vj,
hjust = hj, default.units = "npc") {
textGrob(splitString(label, width), x, y, hjust = hjust, vjust
= vjust, ...,
default.units = default.units, gp = gpar(fontsize = size,
col = colour, fontfamily = family, fontface = face,
lineheight = lineheight), rot = angle)
}, class = "theme", type = "title", call = match.call())
}
qplot(1:10, 1:10) +
opts(title=title, plot.title=theme_title(width=unit(10, "cm")))
I'm not sure how one is supposed to define theme_*** elements though;
perhaps the above is not supposed to have type="title" but "text", I
dunno.
HTH,
baptiste