Setting separate y labels for a facet_wrap()'ed plot.

561 views
Skip to first unread message

Manuel Holtgrewe

unread,
Feb 19, 2010, 7:31:10 AM2/19/10
to ggplot2
Hi

I have wrapped a plot for data of the form

(x value, group value, value name, y1 value, y2 value, y3 value)

with facet_wrap() into 3 plots in a column. One line is printed for
each "group value" in each of the three plots, one for each "value
name", where "y1 value" gives the value in the first plot, etc.

Each shows one dimension of the data. Is there a way to specify a y
label for each part of the plot?

Bests,
Manuel

hadley wickham

unread,
Feb 19, 2010, 9:10:54 AM2/19/10
to Manuel Holtgrewe, ggplot2
Hi Manuel,

That's not currently possible.

Hadley

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> To post to this group, send email to ggp...@googlegroups.com
> To unsubscribe from this group, send email to
> ggplot2+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/ggplot2

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

Manuel Holtgrewe

unread,
Feb 21, 2010, 7:09:36 AM2/21/10
to ggplot2
How hard would it be to add this into ggplot2? Would you be willing to
incorporate a patch for this if I contributed it? I am interested to
add this if it is not too hard.

As far as I can see, it would either mean putting the unit into the
data.frame to be plotted or to give a mapping from factor value to
unit. The second option makes more sense to me.

Then, the actual drawing code would have to add a possible margin
within each facet. When such a mapping is given, the margin would be
calculated based on the unit label height. The actual code for drawing
the labels could be adapted from the existing code to draw labels on
the y axis.

Bests,
Manuel

baptiste auguie

unread,
Feb 21, 2010, 8:46:31 AM2/21/10
to Manuel Holtgrewe, ggplot2
Hi,

It seems to me that it'd be much easier to create independent graphs
and arrange them on a page with a grid layout. That way you have full
control over the labels, etc. for each plot, and from what I heard the
next version of ggplot2 might give you the control over the plot
dimensions required to align the plots more exactly.

A dummy example follows,

library(ggplot2)

d <- data.frame(x=rep(1:10, 3), y=rnorm(30),
f=factor(rep(letters[1:3], each=10)))

# using facet_wrap, only one y label
qplot(x, y, data=d)+facet_wrap(~f, ncol=1)

dev.new()

## library(ggExtra)
source("http://gridextra.googlecode.com/svn/trunk/R/arrange.r")

# using a grid layout
do.call(arrange, c(dlply(d, .(f), function(.d) qplot(x, y, data=.d,
ylab=unique(.d$f))), list(ncol=1)))


Best,

baptiste

Manuel Holtgrewe

unread,
Feb 21, 2010, 9:02:12 AM2/21/10
to baptiste auguie, ggplot2
Baptiste,

thanks for your proposal. I did not know arrange yet.

Hm, this way, I would have to disable the ticks and labels for all but
the bottom chart and manually insert headings. Additionally, as you
write, I would have to wait for a future version so I can make the
subplots align at the same place horizontally.

Actually, I'd like to give extending ggplot2 a try since I could
achieve the same as your proposal by generating three plots and
including them in the publication in a column.

Bests,
-- Manuel

hadley wickham

unread,
Feb 24, 2010, 10:43:38 AM2/24/10
to Manuel Holtgrewe, ggplot2
> How hard would it be to add this into ggplot2? Would you be willing to
> incorporate a patch for this if I contributed it? I am interested to
> add this if it is not too hard.

Very hard. There are two roadbload.

1) Coming up with a nice syntax (i.e. one that I like) for specifying
the different labels

2) Modifying the layout code to draw the labels. This is currently
particularly difficult because I'm in the middle of a rewrite of the
layout code (to hopefully make it both simpler and faster), but the
interaction of all the possible layout options will always require the
code to be pretty complex.

Hadley

Reply all
Reply to author
Forward
0 new messages