position of x-axis tick marks when using facet grid

1,056 views
Skip to first unread message

Miriam

unread,
Aug 16, 2011, 2:40:05 PM8/16/11
to ggplot2
Hi everyone,

I have a question about x-axis tick marks and the positioning of axis
text when using a facet grid.

I'm using the following as part of a function (p1 is a plot that was
previously defined):

p1 <- p1 + facet_grid(X1 ~ hierarchy, space="free", scales="free_x")
p1 <- p1 + opts(axis.text.x = theme_text(angle=45, size=8, hjust=1),
legend.position="none")

When I do this, the tick marks for the x-axis are offset from the
bottom of the x-axis by a different amount for each facet, depending
on the length of the labels associated with each tick mark. If I set
angle=0, this does not happen, but my x-axis labels then become
unreadable.

What is particularly bizarre about this behavior is that it only
happens when I generate this plot using R installed on Linux. Using
the exact same function on a Mac gives x-axis tick marks/labels that
are aligned perfectly. I am using v0.8.9.

Any thoughts?

Thanks in advance!
Miriam

Brian Diggs

unread,
Aug 16, 2011, 5:25:14 PM8/16/11
to ggplot2
On 8/16/2011 11:40 AM, Miriam wrote:
> Hi everyone,
>
> I have a question about x-axis tick marks and the positioning of axis
> text when using a facet grid.
>
> I'm using the following as part of a function (p1 is a plot that was
> previously defined):
>
> p1<- p1 + facet_grid(X1 ~ hierarchy, space="free", scales="free_x")
> p1<- p1 + opts(axis.text.x = theme_text(angle=45, size=8, hjust=1),
> legend.position="none")

Not having p1, this is not reproducible. Working backwards from some
things you said, I realized the x axis is categorical and contains
strings of different lengths. This likely is important. Here is a
reproducible data set:

set.seed(1234)
DF <- data.frame(x=replicate(20, paste(letters[sample(sample(26,1))],
collapse="")),
y=rnorm(20),
X1=rep(c("a","b"), times=10), hierarchy=rep(c("d","e"), each=10))

p1 <- ggplot(DF, aes(x,y)) + geom_point()

> When I do this, the tick marks for the x-axis are offset from the
> bottom of the x-axis by a different amount for each facet, depending
> on the length of the labels associated with each tick mark. If I set
> angle=0, this does not happen, but my x-axis labels then become
> unreadable.

I do see this with

p1 + facet_grid(X1 ~ hierarchy, space="free", scales="free_x") +
opts(axis.text.x = theme_text(angle=45, size=8, hjust=1))

p1 + facet_grid(X1 ~ hierarchy, space="free", scales="free_x") +
opts(axis.text.x = theme_text(angle=45, size=8, hjust=0))

p1 + facet_grid(X1 ~ hierarchy, space="free", scales="free_x") +
opts(axis.text.x = theme_text(angle=90, size=8, hjust=1))

but not with

p1 + facet_grid(X1 ~ hierarchy, space="free", scales="free_x") +
opts(axis.text.x = theme_text(angle=0, size=8, hjust=1))

as you say. It is also present in the development version.

It is also sensitive to the exact data, since I did not see it when I
ran this same code with set.seed(123). I don't know what causes it, but
I wanted to at least confirm it and give a reproducible example.

> What is particularly bizarre about this behavior is that it only
> happens when I generate this plot using R installed on Linux. Using
> the exact same function on a Mac gives x-axis tick marks/labels that
> are aligned perfectly. I am using v0.8.9.
>
> Any thoughts?
>
> Thanks in advance!
> Miriam


--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University

Hadley Wickham

unread,
Aug 16, 2011, 5:36:57 PM8/16/11
to Brian Diggs, ggplot2
Is it the same as this? https://github.com/hadley/ggplot2/issues/221

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/

Brian Diggs

unread,
Aug 16, 2011, 7:07:47 PM8/16/11
to ggplot2
On 8/16/2011 2:36 PM, Hadley Wickham wrote:
> Is it the same as this? https://github.com/hadley/ggplot2/issues/221

Yes.

> Hadley

>> Oregon Health& Science University


>>
>> --
>> 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 ggplot2-/JYPxA39Uh5...@public.gmane.org
>> To unsubscribe: email ggplot2+unsubscribe-/JYPxA39Uh5...@public.gmane.org
>> More options: http://groups.google.com/group/ggplot2
>>
>
>
>


--

Miriam Friedel

unread,
Aug 17, 2011, 9:14:19 AM8/17/11
to ggplot2
Thank you very much, Brian and Hadley.

Brian, as you commented on the github issue (and figured out through your test code): each box is only wide enough for its own widest label. The tick marks are then put just to the right of this box.

This is exactly what is happening with my data.

It looks like the issue has been addressed in one of the forks, and will be in v0.9.

Thanks again,
Miriam
Reply all
Reply to author
Forward
0 new messages