can I make the facet/strip text wrap around?

2,121 views
Skip to first unread message

alienk8

unread,
Apr 7, 2011, 6:03:17 AM4/7/11
to ggplot2
I found this code for wrapping text on stackoverflow:

wrapper <- function(x, ...) paste(strwrap(x, ...), collapse = "\n")

my_title <- "This is a really long title of a plot that I want to
nicely wrap and fit onto the plot without having to manually add the
backslash n" r + geom_smooth() + opts(title = wrapper(my_title, width
= 20))

I would like to use it to wrap the text in a facet/strip but don't
know how.

p+geom_bar(stat="identity")
+facet_wrap(~variable1)+opts(strip.text.x=theme_text(size=12,
face="bold")

Is it passed to the strip.text.x options or the facet_wrap options?

Brandon Hurr

unread,
Apr 7, 2011, 6:33:17 AM4/7/11
to alienk8, ggplot2
You could try...
plot+labs(x=wrapper(my_title))

I tried it and it didn't quite seem to be wrapping properly. You may need to adjust the length of the string before it's split. 

Brandon

Brandon Hurr

unread,
Apr 7, 2011, 6:37:18 AM4/7/11
to alienk8, ggplot2
My bad, that was totally unhelpful. 

You said strip text and I thought axis text. With the labs() function that won't work. 

B

Brandon Hurr

unread,
Apr 7, 2011, 6:46:54 AM4/7/11
to alienk8, ggplot2
It looks like one way could be to use a labeller in the facet call. 


I'm not sure how your wrapper function to work with the labeller though. 

Brandon

alienk8

unread,
Apr 7, 2011, 7:24:18 AM4/7/11
to ggplot2
Thanks, I tried this approach but got an error:

Error in get(as.character(FUN), mode = "function", envir = envir) :
variable names are limited to 256 bytes

On Apr 7, 6:46 am, Brandon Hurr <brandon.h...@gmail.com> wrote:
> It looks like one way could be to use a labeller in the facet call.
>
> http://stackoverflow.com/questions/2403466/greek-letters-in-ggplot-st...
>
> I'm not sure how your wrapper function to work with the labeller though.
>
> Brandon
>
> On Thu, Apr 7, 2011 at 11:37, Brandon Hurr <brandon.h...@gmail.com> wrote:
> > My bad, that was totally unhelpful.
>
> > You said strip text and I thought axis text. With the labs() function that
> > won't work.
>
> > B
>
> > On Thu, Apr 7, 2011 at 11:33, Brandon Hurr <brandon.h...@gmail.com> wrote:
>
> >> You could try...
> >> plot+labs(x=wrapper(my_title))
>
> >> I tried it and it didn't quite seem to be wrapping properly. You may need
> >> to adjust the length of the string before it's split.
>
> >> Brandon
>

Brandon Hurr

unread,
Apr 7, 2011, 8:37:54 AM4/7/11
to alienk8, ggplot2
#create simple dataframe for plotting
xy <- data.frame(x = rep(1:10, times=3), y = rep(10:1, times=3), type = rep(c("this is a really long factor level name that will screw things up hopefully, but I'm not sure if I can wrap this text with wrapper", "other title"), each=5), type2 = rep(LETTERS[3:5], each=10))

#create base plot
plot <- ggplot(data = xy)+
geom_point(aes(x = x, y = y))+
facet_grid(type ~ type2, labeller= wrapper)

#plot base plot
plot

Gives me a slightly different error... 
> plot
Error in if (nc[currentIndex] == 0L) upperBlockIndex <- c(upperBlockIndex,  : 
  missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In Ops.factor(width, nchar(initial, type = "w")) :
  - not meaningful for factors
2: In Ops.factor(width, nchar(prefix, type = "w")) :
  - not meaningful for factors




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

Luciano Selzer

unread,
Apr 7, 2011, 8:58:36 AM4/7/11
to Brandon Hurr, alienk8, ggplot2
Hi all, looking at label_parsed I modified wrapper into label_wrapper and now works partially. The thing is, facetter passes and wants a list. But I don't know how to pass the width to strwrap. Any ideas?

label_wrapper <- function(variable, value, ...) {
   llply(as.character(value), function (x) paste(strwrap(x, ...), collapse = "\n"))
}


Luciano


2011/4/7 Brandon Hurr <brando...@gmail.com>
#create simple dataframe for plotting
xy <- data.frame(x = rep(1:10, times=3), y = rep(10:1, times=3), type = rep(c("this is a really long factor level name that will screw things up hopefully, but I'm not sure if I can wrap this text with wrapper", "other title"), each=5), type2 = rep(LETTERS[3:5], each=10))

#create base plot
plot <- ggplot(data = xy)+
geom_point(aes(x = x, y = y))+
facet_grid(type ~ type2, labeller= label_wrapper, widht = 20)

Brandon Hurr

unread,
Apr 7, 2011, 9:07:32 AM4/7/11
to Luciano Selzer, alienk8, ggplot2
The width would have to come from the innards of ggplot so that it can readjust itself when the window is resized. If you make it a fixed value, you'd have to make sure the output size and aspect ratio was constant across your plots. 

Not sure where to look at this point... 

Brandon

P.S. I didn't think to look at the label_parsed function, good sleuthing!

Brian Diggs

unread,
Apr 7, 2011, 12:27:37 PM4/7/11
to ggplot2
On 4/7/2011 6:07 AM, Brandon Hurr wrote:
> The width would have to come from the innards of ggplot so that it can
> readjust itself when the window is resized. If you make it a fixed value,
> you'd have to make sure the output size and aspect ratio was constant across
> your plots.
>
> Not sure where to look at this point...
>
> Brandon
>
> P.S. I didn't think to look at the label_parsed function, good sleuthing!

I don't think you can pull this off in general, though you are on the
right track if you could. the label_* functions take the arguments
(variable, value) [both vectors], and return a list or vector (of the
same length) of strings. The problem is, there is no ... that could be
used to pass something through. The best approach I could think would
be using the label_wrapper that Luciano gave, with an additional wrapper
around that which fixes the width to pass to the labeller argument of
the facet:

label_wrapper30 <- function(variable, value) {
label_wrapper(variable, value, 30)
}

Then, following Brandon's code:

plot <- ggplot(data = xy)+
geom_point(aes(x = x, y = y))+

facet_grid(type ~ type2, labeller= label_wrapper30)

> On Thu, Apr 7, 2011 at 13:58, Luciano Selzer<luciano.selzer-Re5...@public.gmane.org>wrote:
>
>> Hi all, looking at label_parsed I modified wrapper into label_wrapper and
>> now works partially. The thing is, facetter passes and wants a list. But I
>> don't know how to pass the width to strwrap. Any ideas?
>>
>> label_wrapper<- function(variable, value, ...) {
>> llply(as.character(value), function (x) paste(strwrap(x, ...), collapse
>> = "\n"))
>> }
>>
>>
>> Luciano
>>
>>

>> 2011/4/7 Brandon Hurr<brandon.hurr-Re5J...@public.gmane.org>

>>>>> On Thu, Apr 7, 2011 at 11:37, Brandon Hurr<brandon.h...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>


>>>> wrote:
>>>>>> My bad, that was totally unhelpful.
>>>>>
>>>>>> You said strip text and I thought axis text. With the labs() function
>>>> that
>>>>>> won't work.
>>>>>
>>>>>> B
>>>>>

>>>>>> On Thu, Apr 7, 2011 at 11:33, Brandon Hurr<brandon.h...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>


>>>> wrote:
>>>>>
>>>>>>> You could try...
>>>>>>> plot+labs(x=wrapper(my_title))
>>>>>
>>>>>>> I tried it and it didn't quite seem to be wrapping properly. You may
>>>> need
>>>>>>> to adjust the length of the string before it's split.
>>>>>
>>>>>>> Brandon
>>>>>
>>>>>>> On Thu, Apr 7, 2011 at 11:03, alienk8<alienz...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>>>>>
>>>>>>>> my_title<- "This is a really long title of a plot that I want to
>>>>>>>> nicely wrap and fit onto the plot without having to manually add
>>>> the
>>>>>>>> backslash n"
>>>>
>>>> --
>>>> 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 ggplot2-/JYPxA39Uh5...@public.gmane.org
>>>> To unsubscribe: email ggplot2+unsubscribe-/JYPxA39Uh5...@public.gmane.org


>>>> More options: http://groups.google.com/group/ggplot2
>>>>
>>>
>>> --
>>> 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 ggplot2-/JYPxA39Uh5...@public.gmane.org
>>> To unsubscribe: email ggplot2+unsubscribe-/JYPxA39Uh5...@public.gmane.org
>>> More options: http://groups.google.com/group/ggplot2
>>>
>>
>>
>


--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University

baptiste auguie

unread,
Apr 7, 2011, 4:47:36 PM4/7/11
to alienk8, ggplot2
Please don't cross-post

http://stackoverflow.com/questions/5574157/r-ggplot2-can-i-make-the-facet-strip-text-wrap-around

baptiste

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

alienk8

unread,
Apr 8, 2011, 9:48:50 AM4/8/11
to ggplot2
Thank you very much, this solved my problem!
> >> 2011/4/7 Brandon Hurr<brandon.hurr-Re5JQEeQqe8AvxtiuMw...@public.gmane.org>
>
> >>>   #create simple dataframe for plotting
> >>> xy<- data.frame(x = rep(1:10, times=3), y = rep(10:1, times=3), type =
> >>> rep(c("this is a really long factor level name that will screw things up
> >>> hopefully, but I'm not sure if I can wrap this text with wrapper", "other
> >>> title"), each=5), type2 = rep(LETTERS[3:5], each=10))
>
> >>> #create base plot
> >>> plot<- ggplot(data = xy)+
> >>> geom_point(aes(x = x, y = y))+
> >>> facet_grid(type ~ type2, labeller= label_wrapper, widht = 20)
>
> >>> #plot base plot
> >>>   plot
>
> >>> Gives me a slightly different error...
> >>>> plot
> >>> Error in if (nc[currentIndex] == 0L) upperBlockIndex<- c(upperBlockIndex,
> >>>   :
> >>>    missing value where TRUE/FALSE needed
> >>> In addition: Warning messages:
> >>> 1: In Ops.factor(width, nchar(initial, type = "w")) :
> >>>    - not meaningful for factors
> >>> 2: In Ops.factor(width, nchar(prefix, type = "w")) :
> >>>    - not meaningful for factors
>
> >>> On Thu, Apr 7, 2011 at 12:24, alienk8<alienz747-Re5JQEeQqe8AvxtiuMw...@public.gmane.org>  wrote:
>
> >>>> Thanks, I tried this approach but got an error:
>
> >>>> Error in get(as.character(FUN), mode = "function", envir = envir) :
> >>>>   variable names are limited to 256 bytes
>
> >>>> On Apr 7, 6:46 am, Brandon Hurr<brandon.h...-Re5JQEeQqe8AvxtiuMw...@public.gmane.org>  wrote:
> >>>>> It looks like one way could be to use a labeller in the facet call.
>
> >>>>>http://stackoverflow.com/questions/2403466/greek-letters-in-ggplot-st.
> >>>> ..
>
> >>>>> I'm not sure how your wrapper function to work with the labeller
> >>>> though.
>
> >>>>> Brandon
>
> >>>>> On Thu, Apr 7, 2011 at 11:37, Brandon Hurr<brandon.h...-Re5JQEeQqe8AvxtiuMw...@public.gmane.org>
> >>>> wrote:
> >>>>>> My bad, that was totally unhelpful.
>
> >>>>>> You said strip text and I thought axis text. With the labs() function
> >>>> that
> >>>>>> won't work.
>
> >>>>>> B
>
> >>>>>> On Thu, Apr 7, 2011 at 11:33, Brandon Hurr<brandon.h...-Re5JQEeQqe8AvxtiuMw...@public.gmane.org>
> >>>> wrote:
>
> >>>>>>> You could try...
> >>>>>>> plot+labs(x=wrapper(my_title))
>
> >>>>>>> I tried it and it didn't quite seem to be wrapping properly. You may
> >>>> need
> >>>>>>> to adjust the length of the string before it's split.
>
> >>>>>>> Brandon
>
> >>>>>>> On Thu, Apr 7, 2011 at 11:03, alienk8<alienz...-Re5JQEeQqe8AvxtiuMw...@public.gmane.org>  wrote:
>
> >>>>>>>> my_title<- "This is a really long title of a plot that I want to
> >>>>>>>> nicely wrap and fit onto the plot without having to manually add
> >>>> the
> >>>>>>>> backslash n"
>
> >>>> --
> >>>> 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 ggplot2-/JYPxA39Uh5TLH3Mboc...@public.gmane.org
> >>>> To unsubscribe: email ggplot2+unsubscribe-/JYPxA39Uh5TLH3Mboc...@public.gmane.org
> >>>> More options:http://groups.google.com/group/ggplot2
>
> >>>   --
> >>> 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 ggplot2-/JYPxA39Uh5TLH3Mboc...@public.gmane.org
> >>> To unsubscribe: email ggplot2+unsubscribe-/JYPxA39Uh5TLH3Mboc...@public.gmane.org
Reply all
Reply to author
Forward
0 new messages