Draw facet labels so they look like x-axis titles?

281 views
Skip to first unread message

Zack Weinberg

unread,
Jun 23, 2011, 8:49:17 PM6/23/11
to ggplot2
I often find that when I'm producing a faceted plot, the facet labels
would make more sense if they were placed as if they were axis titles
for each individual plot, i.e. instead of this


alpha beta gamma
1.00| | |
| | |
0.75| .... | .. |
| . . | .. | ..
0.50| .. | .. | ... ..
| . . | .... |.... .....
0.25| . . | .. |.
|. .. |... |.
0.00\--------------- \--------------- \---------------
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7

I would like to have this

1.00| | |
| | |
0.75| .... | .. |
| . . | .. | ..
0.50| .. | .. | ... ..
| . . | .... |.... .....
0.25| . . | .. |.
|. .. |... |.
0.00\--------------- \--------------- \---------------
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
alpha beta gamma

Is there any way to do that? Something with opts() maybe?

Thanks,
zw

baptiste auguie

unread,
Jun 25, 2011, 7:03:07 AM6/25/11
to Zack Weinberg, ggplot2
I don't think there is an easy way to do this, unfortunately. You
could try the following hack,


library(ggplot2)
library(gridExtra)

p <- ggplot(diamonds, aes(carat, ..density..)) +
geom_histogram(binwidth = 0.2) + facet_grid(. ~ cut) +
labs(x=NULL)

g <- ggplotGrob(p)

gg <- getGrob(g,"axis.title.y.text",grep=TRUE)
ytitle <- if(is.zero(gg) || is.null(gg)) ggplot2:::.zeroGrob else
editGrob(gg, vp=NULL)

gg <- getGrob(g,"axis.text.y.text",grep=TRUE)
ylabel <- if(is.zero(gg) || is.null(gg)) ggplot2:::.zeroGrob else
editGrob(gg, vp=NULL)


category <- levels(diamonds$cut)

labs <- llply(category, textGrob, just=c("centre","bottom"),
gp=gpar(fontsize=10))

my.labels <- do.call(arrangeGrob,
c(labs, list(nrow=1, sub="My x title",
left=rectGrob(width=
grobWidth(ylabel), gp=gpar(col=NA),
x=unit(1, "lines"), just="right"))))

## grid.draw(my.labels)

xlab <- gTree(children=gList(my.labels), cl="mylabels" ,
vp=viewport(width=unit(0.3, "npc")))

heightDetails.mylabels <- function(x)
do.call(max, llply(labs, grobHeight)) + unit(1, "line")

foo <- function()
function(label, x, y)
xlab

p + opts(strip.text.y =theme_blank(),
strip.background=theme_blank()) +
opts( axis.title.x = foo()) +
opts(strip.background = theme_blank(),
strip.text.x = theme_blank())

HTH,

baptiste

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

Hadley Wickham

unread,
Jun 26, 2011, 7:30:14 PM6/26/11
to Zack Weinberg, ggplot2
Not yet, but again this is something on my to do list, and recent
changes have made implementing it much easier.
Hadley

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

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Zack Weinberg

unread,
Jun 26, 2011, 9:30:04 PM6/26/11
to Hadley Wickham, ggplot2
This is good to hear. The analysis I'm doing right now changed so I
don't need it for that anymore, but I expect I will be wanting it
again in the future.

Incidentally, where is the option to set the overall plot title
hiding? Or is there even such a thing? It's mentioned in the theme
settings list but I can't find it anywhere else.

zw

Jake Russ

unread,
Jun 26, 2011, 11:43:08 PM6/26/11
to ggplot2
opts(title = "My Plot Title")

You can find more about the theme elements with examples here:
https://github.com/hadley/ggplot2/wiki/+opts()-List

But also I've added a long series of examples and a (hopefully
complete) list of the theme options to the docs already, so should be
good to go for the next version.
Reply all
Reply to author
Forward
0 new messages