Separate y-axis titles when faceting using scales="free_y"

2,508 views
Skip to first unread message

Joran

unread,
Feb 18, 2011, 12:47:24 AM2/18/11
to ggplot2
Since my last question was answered so quickly and thoroughly, I'll
try another.

I'm doing something like this:

ggplot() + facet_grid(row~col,scales="free_y") + etc.

where the result is a 2xn set of panels. Each column of panels is for
a single subject, but is displaying entirely different data, hence the
free_y. Is there a way to add a title to the y axis of each row
separately, without having to add a bunch of spaces manually and then
tinkering with it to get the spacing right?

Another, more concrete example would be taking Fig 7.5 from Hadley's
ggplot book and adding a separate y-axis title to each of those five
time series, rather than just "value".

Joshua Wiley

unread,
Feb 18, 2011, 2:10:10 AM2/18/11
to Joran, ggplot2
Hi Joran,

I could not come up with a nice way to do what you wanted off the top
of my head, but is this acceptable? I know it is not quite what you
asked for...

###############
require(ggplot2)
em <- melt(economics, id = "date")
mylabs <- paste("Y Label", 1:5)
em$variable <- factor(em$variable, labels = mylabs)
qplot(date, value, data = em, geom = "line", group = variable) +
facet_grid(variable ~ ., scale = "free_y") + ylab(NULL)
###############

Best regards,

Josh

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

--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

baptiste auguie

unread,
Feb 18, 2011, 4:03:01 AM2/18/11
to Joshua Wiley, Joran, ggplot2
Hi,

To place the facet titles on the left side and mimic titles, you could try this,

library(ggplot2)
library(gridExtra)

em <- melt(economics, id = "date")
mylabs <- paste("Y Label", 1:5)
em$variable <- factor(em$variable, labels = mylabs)

p <-


qplot(date, value, data = em, geom = "line", group = variable) +
facet_grid(variable ~ ., scale = "free_y") + ylab(NULL)


labs <- llply(levels(em$variable), textGrob, rot=90)

## add a blank grob "sub" below, as the y title is aligned with the full
## ggplot2 height, not just the panel

my.labels <-
do.call(arrangeGrob,
c(labs, list(ncol=1, left="My y title", sub="")))

## grid.draw(my.labels)

## hack: define the width of my.labels
ylab <- gTree(children=gList(my.labels),
cl="mylabels" )

widthDetails.mylabels <- function(x)
max(stringHeight(levels(em$variable))) + unit(1, "line")

## hack: tweak ggplot2's axis.title.y option to use our gTree
foo <- function()
function(label, x, y) ylab

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

HTH,

baptiste

Dennis Murphy

unread,
Feb 18, 2011, 5:35:48 AM2/18/11
to Joran, ggplot2
Hi:

How about some mixture of Josh's and Baptiste's ideas?

df <- data.frame(subj = rep(1:5, each = 40),
                  var = rep(rep(c('Axis title for variable A',
                                  'Axis title for variable B'), each = 20), 5),
                  time = rep(1:20, 10),
                   y = rnorm(200))
g <- ggplot(df, aes(x = time, y = y))
g + geom_line() + facet_grid(var ~ subj, scales = 'free_y')

HTH,
Dennis

On Thu, Feb 17, 2011 at 9:47 PM, Joran <joran...@gmail.com> wrote:

Joran

unread,
Feb 18, 2011, 1:14:09 PM2/18/11
to ggplot2
Thanks for all the suggestions. Using the facet labels (strip.text) as
y-axis labels (on either side) I guess is the easiest solution,
although it would be a neat feature to be able to specify separate
axis labels in the specific case of employing scales="free_y" or
"free_x".

Jonathan Hall

unread,
Feb 18, 2011, 12:58:08 PM2/18/11
to ggplot2
I am trying to do the same thing, and have just added spaces into my y-
axis title to get things to line up right. However, I do not know how
to hide the facet labels. Can anyone help?

Thank you,

Jonathan Hall
PhD Candidate, Department of Economics
University of Chicago
home.uchicago.edu/~jdhall/

Jonathan Hall

unread,
Feb 18, 2011, 1:27:55 PM2/18/11
to ggplot2
Problem solved, just looked closer at the examples given above. Sorry.
Reply all
Reply to author
Forward
0 new messages