Scales and space in faceting

36 views
Skip to first unread message

Roberto Osorio

unread,
Aug 30, 2009, 9:08:35 PM8/30/09
to ggplot2
According to the documentation in Chapter 7,
"For facet_grid there is an additional parameter called space, which
takes
values "free" or "fixed". When the space can vary freely, each column
(or
row) will have width (or height) proportional to the range of the
scale for that
column (or row)."

I'm getting unpredictable behavior when using scales="free_y",
space="free". Take the following example:

da <- data.frame(
x=rep(1:5, 2),
y=c(1+(0:4), 1+(0:4)/10),
z=rep(1:2, each=5))
da

# x y z
#1 1 1.0 1
#2 2 2.0 1
#3 3 3.0 1
#4 4 4.0 1
#5 5 5.0 1
#6 1 1.0 2
#7 2 1.1 2
#8 3 1.2 2
#9 4 1.3 2
#10 5 1.4 2

qplot(x, y, data=da, group=z, geom="line") +
# scale_y_log10() +
facet_grid(z~., scales="free_y", space="free")

If the facet heights were proportional to their row ranges, the ratio
of the top to bottom facet heights (i.e., z=1 to z=2), should be (5-1)/
(1.4-1) = 4/0.4 = 10. The observed ratio in the resulting plot is
actually a lot smaller than that.

If you uncomment the log10 option, this problem looks worse: the two
heights now look the same! Instead, the heights ratio should be
log10(5)/log10(1.4) (since log10(1) = 0), i.e., 4.78.

Comments? I'm using R 2.9.1 and ggplot2 0.8.3.

Roberto

hadley wickham

unread,
Sep 4, 2009, 2:25:42 PM9/4/09
to Roberto Osorio, ggplot2
Hi Robert,

This looks like a bug. Thanks for the clear reproducible example -
I'll add it to my to do list and take a look at it as soon as I get a
chance.

Regards,

Hadley
--
http://had.co.nz/

Quince Gibson

unread,
Oct 2, 2009, 4:44:52 PM10/2/09
to ggplot2
Loving ggplot2.

Just wondering has the bug in spacing when using faceting been fixed
yet?

The following is giving me a fixed x-axis but a lot of space as
chromosomes get higher.

p <- qplot(POS, -log10(SNP_TSCORE_PVAL), colour=CHR2, data = tdata
[tdata$CHR>0,]) + facet_grid(. ~ CHR2, scales="fixed", space="free")
print(p)

Thanks,
Quince.

Hadley Wickham

unread,
Oct 2, 2009, 9:15:14 PM10/2/09
to Quince Gibson, ggplot2
Could you please send a reproducible example? It's hard to know if
it's the same problem or not.

Hadley

Sent from my iPhone

Quince Gibson

unread,
Oct 5, 2009, 12:32:10 PM10/5/09
to ggplot2
Sorry about that, I was referring to the previous example. Here is a
"hopefully" reproducible example. I would like to create a "manhattan
plot" for Genome-wide association studies using faceting and taking
advantage of the 'space="free"' option when using qplot. POS is base
pair position and needs to be on a fixed x scale so that the distance
between points is important (how far up down the chromosome a
particular point is. Chromosome 1 is the largest and has, for
simplification Positions 0:100. Chr 3 has position 0:40, Chr4 has
positions 0:10.

I expect space=free to break the faceted graphs at their upper postion
limit. This is not happening. Both X and Y scales must be fixed for
publication purpuse

> df <- data.frame(
CHR=rep(1:6,each=3),
POS=c(0,10,20,10,20,30,10,15,20,0,5,10,30,31,32,41,40,42),
SCORE=rep(1:3,6))
> df
CHR POS SCORE
1 1 0 1
2 1 10 2
3 1 20 3
4 2 10 1
5 2 20 2
6 2 30 3
7 3 10 1
8 3 15 2
9 3 20 3
10 4 0 1
11 4 5 2
12 4 10 3
13 5 30 1
14 5 31 2
15 5 32 3
16 6 41 1
17 6 40 2
18 6 42 3
> p <- qplot(POS,SCORE,data=df) + facet_grid(. ~ CHR, scales="fixed", space="free")
> print(p)


Hope this helps,
Quince

On Oct 2, 9:15 pm, Hadley Wickham <h.wick...@gmail.com> wrote:
> Could you please send a reproducible example? It's hard to know if  
> it's the same problem or not.
>
> Hadley
>
> Sent from my iPhone
>

hadley wickham

unread,
Oct 6, 2009, 10:45:43 PM10/6/09
to Quince Gibson, ggplot2
On Mon, Oct 5, 2009 at 11:32 AM, Quince Gibson <quince...@gmail.com> wrote:
>
> Sorry about that, I was referring to the previous example.  Here is a
> "hopefully" reproducible example.  I would like to create a "manhattan
> plot" for Genome-wide association studies using faceting and taking
> advantage of the 'space="free"' option when using qplot.  POS is base
> pair position and needs to be on a fixed x scale so that the distance
> between points is important (how far up down the chromosome a
> particular point is.  Chromosome 1 is the largest and has, for
> simplification Positions 0:100.  Chr 3 has position 0:40, Chr4 has
> positions 0:10.
>
> I expect space=free to break the faceted graphs at their upper postion
> limit. This is not happening.  Both X and Y scales must be fixed for
> publication purpuse

space = "free" if only meaningful if scales = "free", because when
scales = "fixed" all the scales are the same!

Hadley

--
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages