[R] label_wrap_gen question
Brian Diggs
diggsb at ohsu.edu
Wed Aug 8 19:37:54 CEST 2012
On 8/8/2012 12:30 AM, vd3000 wrote:
> Hi, Brian
>
> So, I could only rename the label by myself such as
>
> Light and heavy\ngood vehicles (diesel) -\nGVX
>
> in order to get
>
> Light and heavy
> good vehicles (diesel) -
> GVX
>
> Right?
First an etiquette comment: it is customary to quote context when
replying to the list; nabble does not do this by default (and I don't
know how to enable it since I don't use nabble). I have restored the
context (hopefully correctly).
Now to answer your question. If you don't have many labels and you are
concerned about exactly where the breaks occur, then doing it by hand,
as you have above, would probably be best. If you want an automated
solution that respects existing embedded newlines, then you could modify
the labeller function to do so. Effectively split the strings on
newlines, then wrap each part, and put them back together, restoring the
newlines.
label_wrap_respectn_gen <- function(width = 25) {
force(width)
function(variable, value) {
breakn <- strsplit(as.character(value), "\n")
wrapped <- llply(breakn, strwrap, width=width, simplify=FALSE)
laply(llply(wrapped, lapply, paste, collapse="\n"),
paste, collapse="\n")
}
}
You could roll all this up, since it is simple serial evaluation, but
that may be harder to read and know what it does:
label_wrap_respectn_gen <- function(width = 25) {
force(width)
function(variable, value) {
laply(llply(llply(strsplit(as.character(value), "\n"),
strwrap, width=width, simplify=FALSE),
lapply, paste, collapse="\n"),
paste, collapse="\n")
}
}
This hasn't been tested extensively, but seem to work for your example.
> label_wrap <- label_wrap_respectn_gen(width=15)
> label_wrap(NA, c("Light and heavy good vehicles (diesel) -\nGVX"))
1
"Light and\nheavy good\nvehicles\n(diesel) -\nGVX"
compared to
> label_wrap <- label_wrap_gen(width=15)
> label_wrap(NA, c("Light and heavy good vehicles (diesel) -\nGVX"))
1
"Light and\nheavy good\nvehicles\n(diesel) - GVX"
> On 8/7/2012 8:17 AM, Brian Diggs wrote:
>> On 8/6/2012 9:07 PM, vd3000 wrote:
>>> Hi, all
>>>
>>> I am trying to use the label_wrap_gen function in this website.
>>> https://github.com/hadley/ggplot2/wiki/labeller
>>> I tried to make a long name like this
>>>
>>> Light and heavy good vehicles (diesel) -\nGVX
>>>
>>> f2 = facet_grid(vehicle ~ ., labeller=label_wrap_gen(width=15))
>>>
>>> eventually, I got something like this in my label...
>>> *Light and heavy
>>> good vehicles
>>> (diesel) - GVX*
>>> I suppose the "-n" could break GVX to the next row but it failed...
>>> Is it a bug? or it has been overpowered by "width=15"?? so "-n" could not
>>> function well?
>>>
>>> Eventually I tried f2 = facet_grid(vehicle ~.)
>>>
>>> The "-n" did work and I got
>>> *Light and heavy good vehicles (diesel) -
>>> GVX*
>>>
>>> But it also failed because I could not show all the label properly...
>>>
>>> Anyone has idea about this?
>>> It is freaking me out~ I am sorry I am stupid on R
>>> Thanks in advance.
>>>
>>> VD
>>
>> label_wrap_gen uses strwrap to do the "heavy lifting" of figuring out
>> how to actually split the text into lines. From the help page of
>> strwrap, "Whitespace (space, tab or newline characters) in the input is
>> destroyed.", so this is documented behavior. I don't see an option to
>> strwrap to suppress this behavior.
>>
>>> --
>>> View this message in context:
>>> http://r.789695.n4.nabble.com/label-wrap-gen-question-tp4639364.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>
>>
>
>
> --
> Brian S. Diggs, PhD
> Senior Research Associate, Department of Surgery
> Oregon Health & Science University
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/label-wrap-gen-question-tp4639364p4639539.html
> Sent from the R help mailing list archive at Nabble.com.
>
--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University
More information about the R-help
mailing list