Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2: Splitting facet/strip text into two lines

Tags:

Consider the following ggplot2 graph with long facet/strip text broken in two lines. The text goes outside the area devoted to facet titles.

library(ggplot2)
x <- c(1:3, 1:3)
y <- c(3:1, 1:3)
grp <- c(0, 0, 0, 1, 1, 1)
p <- qplot(x=x, y=y) + geom_line() + facet_wrap(~ grp)
grob <- ggplotGrob(p)
strip.elem.y <- grid.ls(getGrob(grob, "strip.text.x", 
                grep=TRUE, global=TRUE))$name
grob <- geditGrob(grob, strip.elem.y[1], 
        label="First line and\n second line" )
grid.draw(grob)

Is there a way to increase the height of the strip text area ?

like image 337
Marco Sandri Avatar asked Jan 29 '12 10:01

Marco Sandri


1 Answers

ggplot2 supports a built in way of doing this using label_wrap_gen.

x <- c(1:3, 1:3)
y <- c(3:1, 1:3)
grp = c(rep("group 1 with a long name",3),rep("group 2 with a long name",3))
d = data.frame(x = x, y =y, grp = grp)
ggplot(d, aes(x=x,y=y)) + geom_line() + facet_wrap(~ grp, labeller = label_wrap_gen(width=10))
like image 53
groceryheist Avatar answered Sep 23 '22 18:09

groceryheist