Automagic line breaks in graph annotation? Version 0.9.2

684 views
Skip to first unread message

igreg

unread,
Sep 5, 2012, 4:38:30 AM9/5/12
to ggp...@googlegroups.com
dear ggploters

I have a question about an old thread
 
https://groups.google.com/forum/?fromgroups#!searchin/ggplot2/splitString/ggplot2/sg9jA-FeQls/i3GPoiNUzN8J
 
can't reply... don't know why.. so i had to start a new thread
 
baptiste's code below worked fine in version 0.9.1... but not anymore in 0.9.2
 
 
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 get an error message:
 
Element plot.title must be a element_text object.
how can i change baptiste's self made "theme_title" to a working element_text object?
 
thanx for your help!
 
greg

Brandon Hurr

unread,
Sep 5, 2012, 5:25:38 AM9/5/12
to igreg, ggp...@googlegroups.com
I thought this looked familiar....

Also, I got a more helpful error message. 
Error in function (el, elname)  : 
Element plot.title must be a element_text object.
In addition: Warning messages:
1: 'opts' is deprecated.
Use 'theme' instead.
See help("Deprecated") 
2: In opts(title = title, plot.title = theme_title(width = unit(10,  :
  Setting the plot title with opts(title="...") is deprecated. Use labs(title="...") or ggtitle("...") instead.

 #Seems to work
qplot(1:10, 1:10) + labs(title=title, plot.title=theme_title(width=unit(10, "cm")))

Dennis Murphy

unread,
Sep 5, 2012, 6:03:50 AM9/5/12
to igreg, ggp...@googlegroups.com
There is a new theming system in 0.9.2 so some things that used to work in the old system might break in the new one. Plot titling is also affected in this version. The titling itself is moved out of opts() (now theme() in 0.9.2); one now generates plot titles either with the title = argument in labs() or with the new ggtitle() function. This is why Brandon's code works in 0.9.2.

Brandon's solution indicates that one can apply theme functions to individual theme elements in this version of ggplot2 (nice job, BTW!)  Although it works for now, theme_title() will have to be modified to conform to the new theming system in order to survive into future versions. I'll defer to Winston re commentary on applying theme functions to individual theme elements (e.g., plot.title) in the new system. I need to study theme_title and how it behaves in the new system.

A document is in preparation re the new theming system that should be available within the next week. I'm adding a few things to the current version and this example looks like something worth adding. After running it through the ggplot2-dev group for vetting, we'll put it up somewhere and make an announcement to the group.

Dennis



--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Dennis Murphy

unread,
Sep 5, 2012, 8:38:24 AM9/5/12
to Brandon Hurr, igreg, ggp...@googlegroups.com
I played with this a little and discovered you get the same result with

qplot(1:10, 1:10) + labs(title = title)

The theme_title() call appears to be ignored, as I would have expected since plot.title is not an argument of labs(), but instead is one of the ... arguments that is evidently unevaluated. AFAICT thus far, trying to pass theme_title() into element_text() is a no-go. I haven't seen anything thus far indicating that one can pass a theme function to a specific theme element in the new system, but that doesn't mean it's impossible.

The text in the title object contains several \n sequences that are recognized in 0.9.2, so it indicates that the new version can handle multiline titles. Now if it can do the same with expressions that contain \n sequences, I'll really be impressed :)

Dennis

Winston Chang

unread,
Sep 6, 2012, 10:19:12 PM9/6/12
to Dennis Murphy, igreg, ggp...@googlegroups.com
It's definitely not possible anymore to do what the theme_title()
function does, which is to return a function that returns a text grob.

In the old theme system, a theme was a list of functions that returned
grobs. In the new theme system, a themes is a list of lists of
properties.

I haven't tried it, but I think you'd have to run splitString() on the
original title text, and then use that returned text as the title. For
example:

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

newtitle <- splitString(title, unit(10, "cm"))

qplot(1:10, 1:10) + ggtitle(newtitle)



-Winston

igreg

unread,
Sep 7, 2012, 8:20:35 AM9/7/12
to ggp...@googlegroups.com
 
Winston, Dennis & Brandon! Thanks a lot!  :-)
 
greg
Reply all
Reply to author
Forward
0 new messages