Mirroring axis labels

662 views
Skip to first unread message

Dean Scarff

unread,
Jun 15, 2010, 10:27:10 PM6/15/10
to ggp...@googlegroups.com
Consider a facet_grid along the lines of:

ggplot(diamonds, aes(carat, ..density..)) +
geom_histogram(aes(fill = clarity), binwidth = 0.2) +
facet_grid(cut ~ .)

However in my data there are a few more levels of cut, and a wider
range of values of bar heights. I increased the image height until
unit differences in bar heights are clearly visible, but as a result,
the entire image no longer fits on a single screen. Since my graphic
is destined for a scrollable webpage, this shouldn't be a problem: the
entire legend is always visible. However, when viewing the top part
of the image, the x-axis labels are not visible so the scale of the x-
axis is unknown.

I understand Hadley has a couple of features planned that would solve
the problem, such as:

Secondary transformed axes: http://groups.google.com/group/ggplot2/browse_thread/thread/61554482a4064572
The height of my image is between 1 and 2 screen-heights, so at any
scroll level either the top or the bottom of the image is visible.
Hence in my case I could use the identity transformation with the
secondary axis and have labels on the top and the bottom.

Facets spanning multiple pages: http://github.com/hadley/ggplot2/issues/#issue/
86
I'm assuming that would incorporate some sort of logic to repeat
labels on each new page.

What should I do until these features are implemented, though? I'd
like to either print the axis labels on the top and bottom, or repeat
the x-axis labels with every facet. All while keeping a fixed scale
among the facets.

I can see one solution in subsetting the data according to the facet
factor (cut in the example) and generating multiple plots. However,
this is tedious because I'd have to pre-calculate the xlim for the
full set and also trick ggplot2 into assigning the same fill scale,
since the fill levels (clarity in the example) are not fully
represented in every facet in my data (unlike the example).

I'm happy to use viewports, grid.layout et al., but don't know how here.

--
Dean


Hadley Wickham

unread,
Jun 17, 2010, 12:09:09 AM6/17/10
to Dean Scarff, ggp...@googlegroups.com
Hi Dean,

How about just adding a few labels at the top with geom text?

Hadley

On Tuesday, June 15, 2010, Dean Scarff <d...@scarff.id.au> wrote:
> Consider a facet_grid along the lines of:
>
>  ggplot(diamonds, aes(carat, ..density..)) +
>  geom_histogram(aes(fill = clarity), binwidth = 0.2) +
>  facet_grid(cut ~ .)
>

> However in my data there are a few more levels of cut, and a wider range of values of bar heights.  I increased the image height until unit differences in bar heights are clearly visible, but as a result, the entire image no longer fits on a single screen.  Since my graphic is destined for a scrollable webpage, this shouldn't be a problem: the entire legend is always visible.  However, when viewing the top part of the image, the x-axis labels are not visible so the scale of the x-axis is unknown.


>
> I understand Hadley has a couple of features planned that would solve the problem, such as:
>
> Secondary transformed axes: http://groups.google.com/group/ggplot2/browse_thread/thread/61554482a4064572
> The height of my image is between 1 and 2 screen-heights, so at any scroll level either the top or the bottom of the image is visible.  Hence in my case I could use the identity transformation with the secondary axis and have labels on the top and the bottom.
>
> Facets spanning multiple pages: http://github.com/hadley/ggplot2/issues/#issue/86
> I'm assuming that would incorporate some sort of logic to repeat labels on each new page.
>
> What should I do until these features are implemented, though?  I'd like to either print the axis labels on the top and bottom, or repeat the x-axis labels with every facet.  All while keeping a fixed scale among the facets.
>
> I can see one solution in subsetting the data according to the facet factor (cut in the example) and generating multiple plots.  However, this is tedious because I'd have to pre-calculate the xlim for the full set and also trick ggplot2 into assigning the same fill scale, since the fill levels (clarity in the example) are not fully represented in every facet in my data (unlike the example).
>
> I'm happy to use viewports, grid.layout et al., but don't know how here.
>
> --
> Dean
>
>

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

Dean Scarff

unread,
Jun 17, 2010, 1:46:57 AM6/17/10
to Hadley Wickham, ggp...@googlegroups.com
On 17 Jun 2010, at 12:09 , Hadley Wickham wrote:

> Hi Dean,
>
> How about just adding a few labels at the top with geom text?

Here are some reasons:

- have to calculate positions according to the x scale breaks and
the y scales, which is even more tedious if scales = "free_y". This
involves a lot of second-guessing ggplot2.

- geom_texts are masked into the plotting panel, as opposed to axis
labels which are strictly outside the plotting area.

- have to mess around a bit with to get it to match themes
(although I guess this is easy)

- no ticks. I suppose I could use the same philosophy as geom_text
and draw them in manually, but the previous arguments apply recursively.

Where's the drawing code for axes in the ggplot2 source tree? I'm
thinking I could just hack the x-axis drawing code to draw twice, the
second time into the strip_h viewport areas.


--
Dean


Hadley Wickham

unread,
Jun 20, 2010, 9:38:56 PM6/20/10
to ggp...@googlegroups.com
> Where's the drawing code for axes in the ggplot2 source tree?  I'm thinking
> I could just hack the x-axis drawing code to draw twice, the second time
> into the strip_h viewport areas.

Well the code to create the grid guides object is guide_axis in
guides-axis.r, and the code that places them in the correct viewports
is in FacetGrid/FacetWrap$add_guides in facet-grid.r and facet-wrap.r
respectively. However, the code is a mess and is exactly what I'm
working on to get such axes working more generally.

Hadley

Reply all
Reply to author
Forward
0 new messages